【发布时间】:2019-09-15 23:14:53
【问题描述】:
我需要搜索 df 列并从列表中返回所有子字符串。
myList= ['a cat', 'the dog', 'a cow']
example df
'col A'
there was a cat with the dog
the cow was brown
the dog was sick
这会拆分列表中的单词并且只返回单个单词
df['col B'] = df['col A'].apply(lambda x: ';'.join([word for word in x.split() if word in (myList)]))
还尝试添加一个 np any...
df['col B'] = df['col A'].apply(lambda x: ';'.join(np.any(word for word in df['col A'] if word in (myList))))
需要退货
'col B'
a cat;the dog
NaN
the dog
【问题讨论】:
标签: python pandas numpy lambda