[英]compare two data frames and add new column to dataframe based on mask values
I am comparing two data frames based on there ids and then merging them using below code: 我正在根据那里的ID比较两个数据帧,然后使用以下代码合并它们:
df = df1.merge(df2, on=id, suffixes=('_x','_y'))
df1 df1
name age id salary
0 Smith 30 2 2000
1 Ron 24 3 30000
2 Mike 35 4 40000
3 Jack 21 5 5000
4 Roshan 20 6 60000
5 Steve 45 8 8000
6 Peter 28 1 1000
df2 df2
name age salary id
0 Peter 32 10000 1
1 Smith 30 1500 2
2 Ron 24 7000 3
3 Mike 35 20000 4
4 Jack 21 5000 5
5 Cathy 20 9000 6
6 Steve 45 56000 8
o/p o / p
name_x age_x id salary_x name_y age_y salary_y
0 Smith 30 2 2000 Smith 30 1500
1 Ron 24 3 30000 Ron 24 7000
2 Mike 35 4 40000 Mike 35 20000
3 Jack 21 5 5000 Jack 21 5000
4 Roshan 20 6 60000 Cathy 20 9000
5 Steve 45 8 8000 Steve 45 56000
6 Peter 28 1 1000 Peter 32 10000
Now based on the output i am comparing _x and _y column values and putting it into mask: 现在基于输出,我正在比较_x和_y列的值并将其放入mask中:
mask = df[cols + '_x'].values == df[cols + '_y'].values
print(mask)
mask o/p 面罩o / p
[[ True True False]
[ True True False]
[ True True False]
[ True True True]
[ True False False]
[ True True False]
[False True False]]
Based on this mask value i want to put condition that if false is present at let say mask[1] it should give me cumulative result of 'No MAtch' that i can append to my output results like: 基于此掩码值,我想提出以下条件:如果在mask [1]中出现false,它应该给我累积的“无匹配”结果,可以将其附加到输出结果中,例如:
name_x age_x id salary_x name_y age_y salary_y new_column
0 Smith 30 2 2000 Smith 30 1500 No Match
1 Ron 24 3 30000 Ron 24 7000 No Match
2 Mike 35 4 40000 Mike 35 20000 No Match
3 Jack 21 5 5000 Jack 21 5000 MAtch
4 Roshan 20 6 60000 Cathy 20 9000 No Match
5 Steve 45 8 8000 Steve 45 56000 No Match
6 Peter 28 1 1000 Peter 32 10000 No Match
matches = ['Match' if x else 'No Match' for x in np.all(mask, axis = -1)]
将为您提供'Match'
和'No Match'
值的数组,您可以通过以下方式将其添加到数据框中:
df['newColumnName'] = matches
Use numpy.where
with numpy.all
for fast vectorized solution: 将
numpy.where
与numpy.all
使用以进行快速矢量化解决方案:
mask = df[cols + '_x'].values == df[cols + '_y'].values
df['new_column'] = np.where(np.all(mask, axis=1) , 'Match','No Match')
print (df)
name_x age_x id salary_x name_y age_y salary_y new_column
0 Smith 30 2 2000 Smith 30 1500 No Match
1 Ron 24 3 30000 Ron 24 7000 No Match
2 Mike 35 4 40000 Mike 35 20000 No Match
3 Jack 21 5 5000 Jack 21 5000 Match
4 Roshan 20 6 60000 Cathy 20 9000 No Match
5 Steve 45 8 8000 Steve 45 56000 No Match
6 Peter 28 1 1000 Peter 32 10000 No Match
Thanks for comment @markuscosinus, if need compare by second 'column'
of mask seelct by indexing - here by mask[:, 1]
: 感谢您的评论@markuscosinus,如果需要通过索引索引将掩码的第二个
'column'
进行比较-此处通过mask[:, 1]
:
df['new_column'] = np.where(mask[:, 1] , 'Match','No Match')
Convert the mask to numpy array or a dataframe, or it should already be in this format: 将掩码转换为numpy数组或数据框,或者应该已经采用以下格式:
mask = pd.DataFrame([[ True, True, False],
[ True, True, False],
[ True, True, False],
[ True, True, True],
[ True, False, False],
[ True, True, False],
[False, True, False]])
And then the following code give you the column you want: 然后下面的代码为您提供所需的列:
mask.apply(sum, axis=1).apply(lambda x: 'Match' if x==3 else 'No Match')
You can add this column to df
. 您可以将此列添加到
df
。
Hope it helps ... :) 希望能帮助到你 ... :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.