Filter dataframe index on multiple conditions

安稳与你 提交于 2021-02-10 05:01:34

问题


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) 

would filter on columns with bbi in their name, but how would I filter on columns containing 'bbi' OR 'abc' ?

A few options that fail:

df.filter(like='bbi' or 'abc', axis=1) 

df.filter(like=('bbi' or 'abc'), axis=1) 

回答1:


I would do the below:

Setup:

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

Solution:

Using 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



回答2:


Not familiar with the filter command. But you could achieve what you want like this I think:

df[(df['column'].str.contains('bbi', case=False)) | (df['column'].str.contains('abc', case=False))]



回答3:


Please find the attached screenshot.

Regex search is slower. So we keep regex=False.

Hope this helps.Thank you.



来源:https://stackoverflow.com/questions/58752330/filter-dataframe-index-on-multiple-conditions

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!