0

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   []
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
palapara
  • 15
  • 1
  • 1
    Does this answer your question? [How to determine whether a Pandas Column contains a particular value](https://stackoverflow.com/questions/21319929/how-to-determine-whether-a-pandas-column-contains-a-particular-value) – Tomerikoo Jan 31 '21 at 21:39
  • thanks! I tried PieCot's answer, and it works perfectly! – palapara Feb 01 '21 at 02:44
  • Please see [What should I do when someone answers my question?](https://stackoverflow.com/help/someone-answers) – Tomerikoo Feb 01 '21 at 08:20

1 Answers1

2

You were very close:

df['Names2'] = df['Names'].map(lambda x: [y for y in kw if y in x])
PieCot
  • 3,564
  • 1
  • 12
  • 20