如果列包含字符串,则返回列表

时间:2021-01-31 21:37:37

标签: python python-3.x pandas

我想检查 Names 列是否包含 kw 中的任何字符串。如果是,返回列表。

这是数据:

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

我试过了:

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

但它返回:

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

我期待输出如下:

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

1 个答案:

答案 0 :(得分:2)

你非常接近:

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