我有关于 3 种水果及其数量的数据。我想从我的数据中排除“Apple”和“Pear”,但遇到以下错误。为什么会这样?
import pandas as pd
df=(pd.DataFrame({'Fruit':['Apple','Orange','Pear','Apple','Orange']
,'Qty':[3,4,1,7,9]}))
df=df[df['Fruit'].str.contains('Apple'|'Pear')==False]
print(df)
TypeError: unsupported operand type(s) for |: 'str' and 'str'
最佳答案
|
运算符应该在字符串本身中:
df[df['Fruit'].str.contains('Apple|Pear')==False]
或者,使用否定运算符:
df[~df['Fruit'].str.contains('Apple|Pear')]
Out:
Fruit Qty
1 Orange 4
4 Orange 9
关于python - 从数据中排除 2 个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43435071/