Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 检查dataframe中的列是否包含字符串列表中的任何项_Python_Pandas - Fatal编程技术网

Python 检查dataframe中的列是否包含字符串列表中的任何项

Python 检查dataframe中的列是否包含字符串列表中的任何项,python,pandas,Python,Pandas,我的目标是检查我的dataframe列,如果该列包含字符串列表中的项(在ex中为匹配项),那么我希望创建一个包含所有匹配项的新dataframe 使用我当前的代码,我能够获取匹配列的列表,然而,它将其作为一个列表,并且我希望使用我以前的信息创建一个新的数据帧 这是我当前的代码,而不是生成一个列表,我想要我以前拥有的所有数据帧信息 matches = ['beat saber', 'half life', 'walking dead', 'population one'] checking = [

我的目标是检查我的dataframe列,如果该列包含字符串列表中的项(在ex中为匹配项),那么我希望创建一个包含所有匹配项的新dataframe

使用我当前的代码,我能够获取匹配列的列表,然而,它将其作为一个列表,并且我希望使用我以前的信息创建一个新的数据帧

这是我当前的代码,而不是生成一个列表,我想要我以前拥有的所有数据帧信息

matches = ['beat saber', 'half life', 'walking dead', 'population one']
checking = []
for x in hot_quest1['all_text']:
    if any(z in x for z in matches):
        checking.append(x)

熊猫通常允许您过滤数据帧,而无需借助
进行循环

这是一种可行的方法:

matches=['beat saber','half life','walking dead','population one']
#matches_regex是一个正则表达式,表示任何字符串:
#“击败马刀|半条命|行尸走肉|人口一”
匹配项_regex=“|”。加入(匹配项)
#matches_bools将是一系列布尔值,指示是否匹配
#对于系列中的每个项目
matches\u bools=hot\u quest1.all\u text.str.contains(matches\u regex,regex=True)
#然后,您可以使用这一系列布尔值来派生新的数据帧
#仅包含匹配行的
匹配的行=热任务1[匹配的行]
以下是
str.contains
方法的文档。

这正是我想要的。非常感谢。