gpt4 book ai didi

python-3.x - 检查一列值是否在 Pandas 的另一列中

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

我想将同一数据框中的一列与另一列进行比较。不仅仅是在相邻值之间,而是遍历 Col1 中的每个值,看看它是否存在于 Col2 值中。

Col1        Col2    exists
cat pig true
a cat false
pig b true
mat axe false

谢谢。

最佳答案

Col1_value = set(df['Col1'].unique())
df['exists'] = df['Col2'].map(lambda x : True if x in Col1_value else False)

关于python-3.x - 检查一列值是否在 Pandas 的另一列中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59675352/

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