比较列pandas

时间:2015-10-27 15:15:19

标签: python pandas

我在两个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)

1 个答案:

答案 0 :(得分:2)

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