Reputation: 781
Creating a dataframe using subsetting with below conditions
subset_df = df_eq.loc[(df_eq['place'].str.contains('Chile')) & (df_eq['mag'] > 7.5),['time','latitude','longitude','mag','place']]
Want to replicate the above subset using query() in Pandas.However not sure how to replicate str.contains() equivalent in Pandas query. "like" in query doesn't seem to work
query_df = df_eq[['time','latitude','longitude','mag','place']].query('place like \'%Chile\' and mag > 7.5')
place like '%Chile'and mag >7.5
^
SyntaxError: invalid syntax
Any help will be appreciated
Upvotes: 17
Views: 17354
Reputation: 4510
Using str.contains works for me in pandas 1.0.0 with this syntax:
df.query("columnA == 'foo' and columnB.str.contains('bar')")
Upvotes: 9
Reputation: 1390
As of now I am able to do this by using the engine='python'
argument of the .query
method to use str.contains
inside a query.
This should work:
query_df = df_eq[['time', 'latitude', 'longitude', 'mag', 'place']].query(
"place.str.contains('Chile') and mag > 7.5", engine="python")
Upvotes: 14
Reputation: 111
What I think is going on here is that you are not able to utilize the method str.contains
within the query
pandas method. What you can do is create a mask and refer to that mask from within query
using the at sign (@). Try this:
my_mask = df_eq["feature"].str.contains('my_word')
df_eq.query("@my_mask")
Upvotes: 10