首页 > 解决方案 > 检查一列是否与另一列的列表中的所有对象匹配

问题描述

我有一个带有一列字符串的数据框和另一个带有字符串列表的数据框。

                     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]

我希望找到所有索引,其中的字符串Column 0Column 1. 在这种情况下,输出将如下所示:

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

我知道我可以使用pandas.Series.str.contains,但这只适用于单个列表,我想尽可能避免迭代/循环。

标签: pythonstringpandaspandas-groupby

解决方案


您可以使用列表推导和布尔索引:

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]

推荐阅读