我有一个像这样的数据框架
Title Description
Area 51 Aliens come to earth on the 4th of July.
Matrix Hacker Neo discovers the shocking truth.
Spaceballs A star-pilot for hire and his trusty sidekick must come to the rescue of a princess.
我想选择行包含字空间或外国人在任何标题或说明。
我可以使用单个列选择包含空格的行,但不确定如何包含第二列。
words_of_interest = ["Space", "Aliens"]
df[df["Title"].str.contains("|".join(words_of_interest))]
Title Description
Area 51 Aliens come to earth on the 4th of July.
Spaceballs A star-pilot for hire and his trusty sidekick must come to the rescue of a
2条答案
按热度按时间goqiplq21#
您可以在两列上应用
str.contains
,然后使用any(axis=1)
聚合布尔掩码:输出:
6bc51xsx2#
以下是其中一个选项:
另一个使用
re
和 listcomp 的例子:输出: