【发布时间】:2013-08-01 03:06:32
【问题描述】:
如果我有这样的框架
frame = pd.DataFrame({
"a": ["the cat is blue", "the sky is green", "the dog is black"]
})
我想检查这些行是否包含某个单词,我只需要这样做。
frame["b"] = (
frame.a.str.contains("dog") |
frame.a.str.contains("cat") |
frame.a.str.contains("fish")
)
frame["b"] 输出:
0 True
1 False
2 True
Name: b, dtype: bool
如果我决定列出一个清单:
mylist = ["dog", "cat", "fish"]
如何检查行是否包含列表中的某个单词?
【问题讨论】:
-
接受的答案中的方法会找到,例如,单词“there”中的子字符串“the”。有关查找 exact 单词的方法,请参见此处:Creating a new column by finding exact word in a column of strings
标签: python python-2.7 pandas