简体   繁体   English

检查一列是否与另一列的列表中的所有对象匹配

[英]Check if one column matches all objects in a list from another column

I have a dataframe with one column of strings and another with a list of strings. 我有一个带有一列字符串的数据框,另一列有一个字符串列表。

                     0                     1
0      apples are good      [orange, banana]
1     bananas are good        [bananas, bad]
2  cucumbers are green      [cucumbers, are]
3     grapes are green  [grapes, are, green]
4     oranges are good             [oranges]
5   pineapples are big     [flowers, apples]

I want the find all the indices where the string in Column 0 matches all the list contents in Column 1 . 我想要找到所有索引,其中Column 0中的字符串与Column 1中的所有列表内容匹配。 In this case the output would look like: 在这种情况下,输出如下所示:

                     0                     1
2  cucumbers are green      [cucumbers, are]
3     grapes are green  [grapes, are, green]
4     oranges are good             [oranges]

I know I can use pandas.Series.str.contains but that only works on a single list and I want to avoid iterating/looping if possible. 我知道我可以使用pandas.Series.str.contains但是只能在单个列表上使用,并且我想尽可能避免迭代/循环。

You can use a list comprehension and Boolean indexing: 您可以使用列表理解和布尔索引:

res = df[[all(word in x.split() for word in y) for x, y in zip(df[0], df[1])]]

print(res)

                     0                     1
2  cucumbers are green      [cucumbers, are]
3     grapes are green  [grapes, are, green]
4     oranges are good             [oranges]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 检查一列中的值是否在另一列的列表中 - Check if a value in one column is in a list in another column 检查一个数据框的列名是否与另一个数据框的索引值匹配,并将值填充到新列中 - Check if column name from one dataframe matches with index value of another dataframe and populate value into a new column 从与另一列中的值匹配的列中的列表中删除值 - Removing value from list in column that matches value from another column 如何从一个数据框列到另一列提取与列表的完全匹配? - How to extract exact matches with list from one dataframe column to another one? 如何检查一个pandas列中列表中的所有元素是否存在于另一个pandas列中 - How to check if all the elements in list in one pandas column are present in another pandas column 需要检查 .CSV 的一列与另一列 .CSV 的一列是否匹配 - Need to check .CSV's one column against another .CSV one column for matches 熊猫:如何检查一列中的重复值并从另一列创建成对的值列表 - pandas: how to check for repeated values in one column and create a pairwise list of values from another column 如果第一列匹配,则将行从一个 csv 附加到另一个 - Appending rows from one csv to another if their first column matches 如何从与另一个二维列表的值匹配的二维列表的相邻列中的二维列表中获取值 - How to get a value from a two-dimensional list that is in an adjacent column from one that matches the value of another two-dimensional list 从另一列列表中的特定值填充一个数据框列 - Fill one Dataframe Column from specific value in list of another column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM