如果有条件,请在熊猫中放行

时间:2020-06-17 17:03:25

标签: python pandas dataframe

我试图根据熊猫在“价格”列中的单元格中是否包含“ /”来删除行。我提到了一个问题:Drop rows in pandas if they contains "???"

因此,我尝试了两种代码:

df = df[~df["Price"].str.contains('/')]

df = df[~df["Price"].str.contains('/',regex=False)]

但是,两个代码都给出错误: AttributeError: Can only use .str accessor with string values!

作为参考,我数据框的前几行如下:

    Fruit   Price
0   Apple     3
1   Apple    2/3
2   Banana    2
3   Orange   6/7

我可以知道问题出在哪里,如何解决此问题?非常感谢!

2 个答案:

答案 0 :(得分:1)

尝试一下:

sio.emit('fromWebsite', fr'app.{camelCasePythonFunctionName}();')

答案 1 :(得分:1)

您需要先将价格列转换为字符串,然后再应用此操作。我认为price列没有数据类型字符串

df['Price'] = df['Price'].astype(str)

然后尝试

df = df[~df["Price"].str.contains('/',regex=False)]