我想在“名称”列上的交点处合并以下两个数据框。
假设数据帧1为:
Name subject_id Marks_scored
0 Billy sub1 98
1 Amy sub2 90
2 Bran sub4 87
3 Alice sub6 69
4 Ayoung sub5 78
数据框2为:
Name subject_id Marks_scored
0 Billy sub2 89
1 Brian sub4 80
2 Bran sub3 79
3 Bryce sub6 97
4 Betty sub5 88
我只希望输出为:
Name subject_id Marks_scored
0 Billy sub1 98
1 Bran sub4 87
2 Billy sub2 89
3 Bran sub3 79
答案 0 :(得分:1)
pd.concat([df1, df2], axis=1, join='inner')
OR
pd.merge(df1, df2, on='subject_id', how='inner')
答案 1 :(得分:1)
尝试使用pd.concat
,然后使用duplicated
和布尔索引:
df_out = pd.concat([df1,df2])
df_out[df_out.duplicated('Name', keep=False)]
输出:
Name subject_id Marks_scored
0 Billy sub1 98
2 Bran sub4 87
0 Billy sub2 89
2 Bran sub3 79