gpt4 book ai didi

python - Pandas :两列字符串列表之间的输出差异

转载 作者:行者123 更新时间:2023-12-04 07:25:48 25 4
gpt4 key购买 nike

我有一个包含两列的数据框,如下所示:

df = pd.DataFrame({'pos_1':[['VERB', 'PRON', 'DET', 'NOUN', 'ADP'],['NOUN', 'PRON', 'DET', 'NOUN', 'ADV', 'ADV']],
'pos:2':[['VERB', 'PRON', 'DET', 'NOUN', 'ADP'],['VERB', 'PRON', 'DET', 'NOUN', 'ADV', 'ADV']]})
我正在尝试使用 apply 输出这两列之间的差异。
df['diff'] = df.apply(lambda x: [i for i in x['pos_1'] if i not in x['pos_2']], axis=1)
我想要的 diff 列输出应该是:
diff
1 []
2 ['NOUN','VERB']
但相反,我在 diff 列中得到两个空列表。我不知道我做错了哪一部分

最佳答案

如果需要比较两个列表的元素明智并返回差异,请使用 zip比较每对并最后通过嵌套列表理解将其展平:

f = lambda x: [z for i, j in zip(x['pos_1'],x['pos_2']) if i != j for z in [i, j]]
df['diff'] = df.apply(f, axis=1)
print (df)

pos_1 pos_2 \
0 [VERB, PRON, DET, NOUN, ADP] [VERB, PRON, DET, NOUN, ADP]
1 [NOUN, PRON, DET, NOUN, ADV, ADV] [VERB, PRON, DET, NOUN, ADV, ADV]

diff
0 []
1 [NOUN, VERB]

关于python - Pandas :两列字符串列表之间的输出差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68224138/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com