gpt4 book ai didi

python - 比较两个列表并添加一个包含结果的新列

转载 作者:行者123 更新时间:2023-12-05 08:38:22 27 4
gpt4 key购买 nike

比较两个列表并使用 findKB 添加新列

df = pd.DataFrame({'A': [['10', '20', '30', '40'],['50', '60', '70', '80']], 
'B': [['a', 'b'],['c','d']]})
findKBs = ['10','90']


A B
0 [10, 20, 30, 40] [a, b]
1 [50, 60, 70, 80] [c, d]

这将是期望的行为

                  A       B         C
0 [10, 20, 30, 40] [a, b] [90]
1 [50, 60, 70, 80] [c, d] [10,90]

提前致谢

最佳答案

我们可以使用np.isin

df['C'] = [find_kb[~np.isin(find_kb, a)] 
for a, find_kb in zip(df['A'], np.array([findKBs] * len(df)))]
print(df)
A B C
0 [10, 20, 30, 40] [a, b] [90]
1 [50, 60, 70, 80] [c, d] [10, 90]

或者我们可以使用filter

df['C'] = [list(filter(lambda val: val not in a, find_kb))
for a, find_kb in zip(df['A'],[findKBs] * len(df))]

#df['C'] = df['A'].map(lambda list_a: list(filter(lambda val: val not in list_a,
# findKBs)
# )
# )

filter 更难阅读但更有效:

%%timeit
df['C'] = [list(filter(lambda val: val not in a, find_kb))
for a, find_kb in zip(df['A'],[findKBs] * len(df))]

194 µs ± 10.5 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)



%%timeit
df['C'] = [find[~np.isin(find, a)] for a, find in zip(df['A'], np.array([findKBs] * len(df)))]
334 µs ± 38.6 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

%%timeit
df['C'] = df['A'].map(lambda x: np.setdiff1d(findKBs,x))
534 µs ± 17.7 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

关于python - 比较两个列表并添加一个包含结果的新列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62998785/

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