I created two query strings using str.contains()
, and combine them, then pass it to DataFrame.query()
.
I get an AttributeError: 'dict' object has no attribute 'append'.
Removing regex=False
parameter worked fine but my data contains some '/*'
. So I need this parameter to treat my data as a literal string.
import pandas as pd
df = pd.DataFrame({'city': ['osaka', ], 'food': ['apple', ],})
querystr1 = 'osaka'
querystr2 = 'apple'
querystr1 = "city.str.contains('" + querystr1 + "', regex=False)"
querystr2 = "food.str.contains('" + querystr2 + "', regex=False)"
querystr = querystr1 + ' & ' + querystr2
print(querystr)
value = df.query(querystr, engine='python').index.values.astype(int)
print(value)
print(value.size)
How can I query my dataframe not recognizing regular expressions? Is there a smarter way to do this?
I'm no expert, but removing the regex=False
part worked for me:
import pandas as pd
df = pd.DataFrame({'city': ['osaka', ], 'food': ['apple', ],})
querystr1 = 'osaka'
querystr2 = 'apple'
querystr1 = "city.str.contains('" + querystr1 + "')"
querystr2 = "food.str.contains('" + querystr2 + "')"
querystr = querystr1 + ' & ' + querystr2
print(querystr)
value = df.query(querystr, engine='python').index.values.astype(int)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.