Pandas 比较列表的列

Pandas compare columns of list

我的 DataFrame 中有列存储 lists,我想将列中的每个元素与 lists 进行比较。

我试过的方法都失败了:

df.list_col == ['3', '4']
df.list_col.isin([['3', '4']])
df.list_col.equals(['3', '4'])

有没有简单的解决方法?

您可以将 applyin 一起使用:

df = pd.DataFrame({'A':[[1,2],[2,4],[3,1]],
                   'B':[4,5,6]})

print (df)
        A  B
0  [1, 2]  4
1  [2, 4]  5
2  [3, 1]  6

print (df.A.apply(lambda x: 2 in x))
0     True
1     True
2    False
Name: A, dtype: bool