Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

filter data by str.contains

I'm trying to filter my large data by columns that may contains the following strings 'io' and 'ir'.

df1

index  aio   bir   ckk
1      2     3     4
2      3     4     5

I want to create a new df with columns that contain 'io' and 'ir. The new df should look :

index  aio   bir  
1      2     3    
2      3     4     

I tried

df = df[:, str.contains('io','ir')] 

but I got an error saying type object 'str' has no attribute 'contains'

like image 578
Sam Avatar asked Mar 23 '26 09:03

Sam


2 Answers

with pd.DataFrame.filter

df.filter(regex='i(o|r)')

       aio  bir
index          
1        2    3
2        3    4

If you have a list of things to match

things = ['io', 'ir']
df.filter(regex='|'.join(things))

       aio  bir
index          
1        2    3
2        3    4

Alternatives

df.filter(regex='io|ir')

df.loc[:, df.columns.str.contains('io|ir')]
like image 59
piRSquared Avatar answered Mar 25 '26 21:03

piRSquared


Since you mention str.contains

df.loc[:,df.columns.str.contains('io|ir')]
Out[354]: 
       aio  bir
index          
1        2    3
2        3    4
like image 38
BENY Avatar answered Mar 25 '26 23:03

BENY



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!