我有一个数据帧,其中一列为字符串数组,第二列为一个字符串值。
a = pd.Series([["a","b","c", "d"],["a","b","c", "d"],["a","b","c", "d"],["a","b","c", "d"],["a","b","c", "d"]])
b = pd.Series(["a","d","e", "c", "b"])
我希望检查a中是否包含b,但是运行isin函数时收到错误消息。
b.isin(a)
任何解决方案。我特别想在这里避免循环,不确定是否基于运行时间是一个好的策略。
编辑:
**a b**
["a","b","c", "d"] a
["a","b","c", "d"] d
["a","b","c", "d"] e
["a","b","c", "d"] c
["a","b","c", "d"] b
预期的输出是一系列按行比较的结果。
[True True False True True]
答案 0 :(得分:1)
这应该对您有用:
import pandas as pd
a = pd.Series([["a","b","c", "d"],["a","b","c", "d"],["a","b","c", "d"],["a","b","c", "d"],["a","b","c", "d"]])
b = pd.Series(["a","d","e", "c", "b"])
[x in y for y,x in zip(a,b)]
...的输出是:
[True, True, False, True, True]
答案 1 :(得分:1)
pandas.Series
实现了combine方法,您可以通过以下方式使用该方法来查找b
列表中同样出现的元素:
a
输出:
import pandas as pd
a = pd.Series([["a","b","c", "d"],["a","b","c", "d"],["a","b","c", "d"],["a","b","c", "d"],["a","b","c", "d"]])
b = pd.Series(["a","d","e", "c", "b"])
a.combine(b, lambda a,b: b in a)