【发布时间】:2022-11-16 12:12:32
【问题描述】:
我有一个这样的数据框:
aa bb cc
[a, x, y] a 1
[b, d, z] b 2
[c, e, f] s 3
np.nan d 4
我正在尝试像这样创建一个新列:
aa bb cc dd
[a, x, y] a 1 True
[b, d, z] b 2 True
[c, e, f] s 3 False
np.nan d 4 False
我目前的解决方案是:
def some_function(row):
if row['bb].isin(row['aa'])==True:
return True
return False
df['dd'] = df.apply(lambda row: some_function(row), axis=1)
但这会引发错误("'str' object has no attribute 'isin'", 'occurred at index 0')
我怀疑,因为在检查 isin 时我遗漏了一些东西。
本质上,我需要检查 bb 的 str 值是否在列 aa 中,每个单元格中都有一个列表。
关于如何做到这一点的任何想法?
【问题讨论】: