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
in
operator if you set theengine='python'
. If it works, it will likely end up with a pretty inefficient query (normallypandas
tries to usenumexpr
to speed things up butnumexpr
doesn't support thein
operator ...) – Prizewinnerlike
operator is not yet implemented in pandasquery()
method, so you can't do it usingquery()
method – Couteau.iloc
in this case the following should be enoughdf_eq[(df_eq['place'].str.contains('Chile')) & (df_eq['mag'] > 7.5)][['time','latitude','longitude','mag','place']]
– Prohibitive