Python 如何根据“中的多个值删除所有行”;水果“;专栏?
我有一个简单的数据框架Python 如何根据“中的多个值删除所有行”;水果“;专栏?,python,pandas,dataframe,Python,Pandas,Dataframe,我有一个简单的数据框架 Num Fruit Price 1 Apple 1.00 1 Apple 1.00 2 Apple 1.50 2 Orange 1.50 3 Orange 1.00 3 Banana 0.50 >>> df.drop(df.Fruit.isin(["Apple","Orange"])) Fruit Num Price 2 Apple 2 1.50 3 Orange
Num Fruit Price
1 Apple 1.00
1 Apple 1.00
2 Apple 1.50
2 Orange 1.50
3 Orange 1.00
3 Banana 0.50
>>> df.drop(df.Fruit.isin(["Apple","Orange"]))
Fruit Num Price
2 Apple 2 1.50
3 Orange 2 1.50
4 Orange 3 1.00
5 Banana 3 0.50
我想删除所有有水果的行Apple
或Orange
预期输出应如下所示:
Num Fruit Price
3 Banana 0.50
我尝试执行以下语法,但不知怎的,它没有删除数据帧中的所有行
Num Fruit Price
1 Apple 1.00
1 Apple 1.00
2 Apple 1.50
2 Orange 1.50
3 Orange 1.00
3 Banana 0.50
>>> df.drop(df.Fruit.isin(["Apple","Orange"]))
Fruit Num Price
2 Apple 2 1.50
3 Orange 2 1.50
4 Orange 3 1.00
5 Banana 3 0.50
有没有解决这个问题的建议?您需要传递要删除的行的索引,但您传递的是一个布尔数组。您可以将其更改为:
df.drop(df[df.Fruit.isin(["Apple", "Orange"])].index)
Out:
Num Fruit Price
5 3 Banana 0.5
或者,您可以选择不包含apple或orange的行:
df[~(df.Fruit.isin(["Apple", "Orange"]))]
Out:
Num Fruit Price
5 3 Banana 0.5