如果我有這樣的檢查,如果字符串中的大熊貓據幀列在名單
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']
輸出:
True
False
True
如果我決定做一個列表
mylist =['dog', 'cat', 'fish']
我怎麼會檢查該行包含在列表中的某個單詞?
這顯著加快了我在做什麼。有什麼辦法可以返回子模式(比如說* dog *)而不是真正的錯誤嗎? – zelusp
想通了:返回匹配的模式使用'frame.a.str.extract(pattern)' – zelusp