简体   繁体   中英

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 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)]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM