检查 pandas 字符串列是否包含多个单词,顺序不限

Check if pandas string column contains multiple words, in any order

我正在处理 Twitter 数据并尝试查找包含多个单词的字符串。 以下行适用于一个单词和 OR 条件。

tweets_text[tweets_text.str.contains("break")] #Find strings with the word break

tweets_text[tweets_text.str.contains("break|social|media")] #Find strings with either break or social, or media

我正在尝试查找包含这三个词(“break & social & media”)的字符串

你可以这样拆分它们:

tweets_text.loc[tweets_text.str.contains("break") & tweets_text.str.contains("social") & tweets_text.str.contains("media")]
df = pd.Series(['break', 'break media social', 'break media'])

系列:

0                 break
1    break media social
2           break media

提取:

tweets_text[tweets_text.str.contains('(?=.*break)(?=.*social)(?=.*media)')]

输出:

1    break media social