Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter dataframe index on multiple conditions

Tags:

pandas

filter

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) 
like image 425
ZakS Avatar asked Oct 30 '25 01:10

ZakS


1 Answers

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
like image 168
anky Avatar answered Oct 31 '25 21:10

anky



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!