如何根据在“水果"列中找到的多个值删除所有行? [英] How to Drop All The Rows Based on Multiple Values Found in the "Fruit "Column?

查看:79
本文介绍了如何根据在“水果"列中找到的多个值删除所有行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个简单的数据框

Num 水果价格1 苹果 1.001 苹果 1.002 苹果 1.502 橙 1.503 橙色 1.003 香蕉 0.50

我想删除所有带有水果 AppleOrange

的行

预期的输出应该是这样的:

Num 水果价格3 香蕉 0.50

我尝试执行以下语法,但不知何故它没有删除数据框中的所有行

<预><代码>>>>df.drop(df.Fruit.isin(["Apple","Orange"]))水果数量价格2 苹果 2 1.503 橙色 2 1.504 橙色 3 1.005 香蕉 3 0.50

有什么建议可以解决这个问题吗?

解决方案

您需要传递要删除的行的索引,但您传递的是一个布尔数组.您可以将其更改为:

df.drop(df[df.Fruit.isin(["Apple", "Orange"])].index)出去:数量水果价格5 3 香蕉 0.5

或者您可以选择不包含 apple 或 orange 的行:

df[~(df.Fruit.isin(["Apple", "Orange"]))]出去:数量水果价格5 3 香蕉 0.5

I have this simple 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

I want to drop all the rows which have the fruit Apple or Orange

The expected output should be like this:

Num  Fruit   Price
3    Banana  0.50

I tried to doing the following syntax, but somehow it did not drop all the rows in the dataframe

>>> 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

Any suggestion how to solve this?

解决方案

You need to pass the indices of the rows to be dropped, but you are passing a boolean array. You can change it to:

df.drop(df[df.Fruit.isin(["Apple", "Orange"])].index)
Out: 
   Num   Fruit  Price
5    3  Banana    0.5

Or you can select the rows that don't contain apple or orange:

df[~(df.Fruit.isin(["Apple", "Orange"]))]
Out: 
   Num   Fruit  Price
5    3  Banana    0.5

这篇关于如何根据在“水果"列中找到的多个值删除所有行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆