我使用pandas 0.13.1 Python 2.7:
我在风险列中有一些既不是小,中,高的值.我想删除值不是小,中和高的行.我尝试了以下方法:
df = df[(df.risk == "Small") | (df.risk == "Medium") | (df.risk == "High")]
但是这会返回一个空数据框.我该如何正确过滤它们?
最佳答案
我想你想要:
df = df[(df.risk.isin(["Small","Medium","High"]))]
例:
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','High'])]
Out[6]:
risk
0 Small
1 High
2 Medium
[3 rows x 1 columns]