本文介绍了 pandas 根据多种条件过滤行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用pandas 0.13.1 Python 2.7:
I use pandas 0.13.1 Python 2.7:
我在risk
列中有一些值,都不是Small
,Medium
或High
.我想删除值不为Small
,Medium
和High
的行.我尝试了以下方法:
I have some values in the risk
column that are neither, Small
, Medium
or High
. I want to delete the rows with the value not being Small
, Medium
and High
. I tried the following:
df = df[(df.risk == "Small") | (df.risk == "Medium") | (df.risk == "High")]
但是这将返回一个空的数据帧.如何正确过滤它们?
But this returns an empty data frame. How can I filter them correctly?
推荐答案
我认为您想要:
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','Medium','High'])]
Out[6]:
risk
0 Small
1 High
2 Medium
[3 rows x 1 columns]
这篇关于 pandas 根据多种条件过滤行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!