这一定在其他地方得到了回答,但我找不到链接。我有一个 df
,其中包含一些任意文本和一个单词列表 W
。我想为 df
分配一个新列,以便它包含 W
中匹配的单词。例如,给定 df
T
dog
dog and meerkat
cat
如果 W="dog",那么我想要
T
dog dog
dog and meerkat dog
cat
我目前的情况是
df[df.T.str.contains('|'.join(W), case=False)]
但这只会给我匹配的行,即:
T
dog
dog and meerkat
有什么想法、建议吗?
最佳答案
您可以使用 Series.where
- 哪里不匹配得到 NaN
:
W = 'dog'
df['new'] = df['T'].where(df['T'].str.contains('|'.join(W), case=False))
print (df)
T new
0 dog dog
1 dog and meerkat dog and meerkat
2 cat NaN
W = 'dog'
df.loc[df['T'].str.contains('|'.join(W), case=False), 'new'] = df['T']
print (df)
T new
0 dog dog
1 dog and meerkat dog and meerkat
2 cat NaN
另一种可能的解决方案是 numpy.where
如果不匹配,可以在哪里添加值:
W = 'dog'
df['new'] = np.where(df['T'].str.contains('|'.join(W), case=False), df['T'], 'nothing')
print (df)
T new
0 dog dog
1 dog and meerkat dog and meerkat
2 cat nothing
但是如果只需要匹配列表的值使用extract
并为 groups
添加第一个和最后一个 ()
:
W = ['dog', 'rabbit']
df['new'] = df['T'].str.extract('('+'|'.join(W) + ')', expand=True)
print (df)
T new
0 dog dog
1 dog and meerkat dog
2 cat NaN
关于python - 如何将 `str.contains` 的输出分配给 Pandas 列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41707015/