我有一个包含4783个元素的单词negative
的列表。我想使用以下代码
tweets3 = tweets2[tweets2['full_text'].str.contains('|'.join(negative))]
但是,它给出了类似error: multiple repeat at position 4193
的错误。
我不明白此错误。显然,如果我在str.contains
中使用单个单词,例如str.contains("deal")
,则可以得到结果。
[我需要的是一个新的数据框,它只包含那些行,这些行包含在数据框tweets2
列full_text
中出现的任何单词。
作为选择,我还想看看是否可以有一个boolean
列来表示当前和不存在的值,例如0 or 1
。
我在@ wp78de的帮助下使用以下代码:
tweets2['negative'] = tweets2.loc[tweets2['full_text'].str.contains(r'(?:{})'.format('|'.join(negative)), regex=True, na=False)].copy()
re.escape()
函数。这条线应该足够:.str.contains(r'(?:{})'.format(re.escape('|'.join(words)), regex=True, na=False)]