在多个条件下过滤数据框索引 [英] Filter dataframe index on multiple conditions
本文介绍了在多个条件下过滤数据框索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在 pandas.DataFrame.filter 可以使用参数"like"或"regex",使其支持OR条件.例如:
In pandas.DataFrame.filter is there a way to use the parameters "like" or "regex" so they support an OR condition. for example:
df.filter(like='bbi', axis=1)
将对名称中带有 bbi
的列进行过滤,但是如何过滤包含'bbi'
或'abc'
的列?
would filter on columns with bbi
in their name, but how would I filter on columns containing 'bbi'
OR 'abc'
?
一些失败的选择:
df.filter(like='bbi' or 'abc', axis=1)
df.filter(like=('bbi' or 'abc'), axis=1)
推荐答案
我将执行以下操作:
设置:
df=pd.DataFrame(np.random.randint(0,20,20).reshape(5,4),
columns=['abcd','bcde','efgh','bbia'])
print(df)
abcd bcde efgh bbia
0 10 17 2 7
1 7 12 18 9
2 17 7 11 17
3 14 4 2 9
4 15 10 12 11
解决方案:
使用 df.filter
:
df.filter(regex=r'(abc|bbi)')
abcd bbia
0 10 7
1 7 9
2 17 17
3 14 9
4 15 11
这篇关于在多个条件下过滤数据框索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文