[英]Check if one column matches all objects in a list from another column
我有一个带有一列字符串的数据框,另一列有一个字符串列表。
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 0
中的字符串与Column 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
但是只能在单个列表上使用,并且我想尽可能避免迭代/循环。
您可以使用列表理解和布尔索引:
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.