简体   繁体   中英

return a list if the column contains a string

I would like to check if the Names column contains any of the strings in the kw . If yes, return the list.

Here is the data:

import pandas as pd

df = pd.DataFrame({'Names':['APPLE JUICE','APPLE DRINK','APPLE JUICE DRINK', 'APPLE','ORANGE AVAILABLE','TEA AVAILABLE']})
kw = ['APPLE JUICE', 'DRINK', 'ORANGE', 'LEMONS', 'STRAWBERRY', 'BLUEBERRY', 'TEA COFFEE']

I've tried:

df['Names2'] = df['Names'].apply(lambda x: [k if any([k in x for k in kw]) else ''])

But it returns:

    Names   Names2
0   APPLE JUICE [<function <lambda> at 0x0000017BB875C550>]
1   APPLE DRINK [<function <lambda> at 0x0000017BB875C550>]
2   APPLE JUICE DRINK   [<function <lambda> at 0x0000017BB875C550>]
3   APPLE   []
4   ORANGE AVAILABLE    [<function <lambda> at 0x0000017BB875C550>]
5   TEA AVAILABLE   []

I am expecting an output like:

    Names   Names2
0   APPLE JUICE ['APPLE JUICE']
1   APPLE DRINK ['DRINK']
2   APPLE JUICE DRINK   ['APPLE JUICE', 'DRINK']
3   APPLE   []
4   ORANGE AVAILABLE    ['ORANGE']
5   TEA AVAILABLE   []

You were very close:

df['Names2'] = df['Names'].map(lambda x: [y for y in kw if y in x])

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