gpt4 book ai didi

python - 比较 pandas 列

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

我在两个 DataFrame 中有四列,我想检查列中的 id1 = id2 和 count1 = count2 是否相同,如果匹配则结果为 1,如果不匹配则结果为 0。然而我的代码只返回0。我认为它不会一一迭代,而是以不同的行号进行迭代。我尝试压缩我想要的列,但我没有看到任何区别。你有什么想法?谢谢你!

import pandas as pd
file1 = 'file1.csv'
file2 = 'file2.csv'

df1 = read_csv(file1)
df1 = read_csv(file2)

id1 = df1['id1']
count1 = df1['count1']
id2 = df2['id2']
count2 = df2['count2']

newresult = pd.concat([id1, count1, id2, count2], axis = 1)
id1 = zip(df1['id1'])
count1 = zip(df1['count1'])

newresult['compare'] = newresult.apply(lambda x: 1 if x['id1'] == x['id2'] and x['count1'] == x['count2'] else 0, axis = 1)

最佳答案

import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randint(0, 2, (50, 4)), columns=["id1", "id2", "count1", "count2"])
df["compare"] = ((df.id1==df.id2) & (df.count1==df.count2)).astype(int)

enter image description here

关于python - 比较 pandas 列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33371720/

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