Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass a list to str.contains - Pandas

I have a pandas related question: I need to filter a column (approx. 40k entries) based on substrings included (or not) in the column. Each of the entries in the column is basically a very long list of attributes (text) which I need to be able to filter individually. This line of code works, but it is not scalable (I have hundreds of attribures I have to filter for):

df[df['Product Lev 1'].str.contains('W1 Rough wood', na=False) & df['Product Lev 1'].str.contains('W1.2', na=False)]

Is there a possibiltiy to insert all the items I have to filter and pass it as a list? Or any similr solution ?

THANK YOU!

like image 375
Blackchat83 Avatar asked Oct 17 '25 14:10

Blackchat83


1 Answers

Like this:

data = {'col_1': [3, 2, 1, 0], 'col_2': ['aaaaDB', 'bbbbbbCB', 'cccccEB', 'ddddddUB']}
df=pd.DataFrame.from_dict(data)
lst = ['DB','CB']  #replace with your list
rstr = '|'.join(lst)
df[df['col_2'].str.upper().str.contains(rstr)]
like image 146
uxke Avatar answered Oct 20 '25 23:10

uxke



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!