Pandas 根据多个条件过滤行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23323974/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 21:58:22  来源:igfitidea点击:

Pandas filter rows based on multiple conditions

pythonpandas

提问by ArtDijk

I use pandas 0.13.1 Python 2.7:

我使用Pandas 0.13.1 Python 2.7:

I have some values in the riskcolumn that are neither, Small, Mediumor High. I want to delete the rows with the value not being Small, Mediumand High. I tried the following:

我在risk列中有一些值既不是 , Small,Medium也不是High。我想删除值不是Small,Medium和 的行High。我尝试了以下方法:

df = df[(df.risk == "Small") | (df.risk == "Medium") | (df.risk == "High")]

But this returns an empty data frame. How can I filter them correctly?

但这会返回一个空的数据框。如何正确过滤它们?

回答by EdChum

I think you want:

我想你想要:

df = df[(df.risk.isin(["Small","Medium","High"]))]

Example:

例子:

In [5]:
import pandas as pd
df = pd.DataFrame({'risk':['Small','High','Medium','Negligible', 'Very High']})
df

Out[5]:

         risk
0       Small
1        High
2      Medium
3  Negligible
4   Very High

[5 rows x 1 columns]

In [6]:

df[df.risk.isin(['Small','Medium','High'])]

Out[6]:

     risk
0   Small
1    High
2  Medium

[3 rows x 1 columns]

回答by Rafael

Another nice and readable approach is the following:

另一种不错的可读方法如下:

small_risk = df["risk"] == "Small"
medium_risk = df["risk"] == "Medium"
high_risk = df["risk"] == "High"

Then you can use it like this:

然后你可以像这样使用它:

df[small_risk | medium_risk | high_risk]

or

或者

df[small_risk & medium_risk]