我想结合两个具有相同索引号但不同列号的datframe:
>>> df1
col_1 col_2 col_3 col_4
0 a x NaN 54
1 a y 5 34
2 b z NaN 64
3 c z 7 23
>>> df2
col_1 col_2 col_3 col_4 col_5
0 a x NaN 14 14
1 b z NaN 9 7
2 c z 7 51 53
3 a y 5 87 66
df2将基于col_1,col_2和col_3的值合并到df1中.
但是行的顺序将不同.
我想根据df1的顺序将它们合并
答案将是这样的:
col_1 col_2 col_3 col_4 col_4 col_5
0 a x NaN 54 14 14
1 a y 5 34 87 66
2 b z NaN 64 9 7
3 c z 7 23 51 53
我不在乎列名,因此你们可以根据需要修改它们.
解决方法:
如果我理解正确,那么您希望不加排序就加入索引:
result = df1.join(df2.drop(columns=['col_1', 'col_2', 'col_3']), lsuffix='x', rsuffix='y', sort=False)
print(result)
输出:
col_1 col_2 col_3 col_4x col_4y col_5
0 a x NaN 54 14 14
1 a y 5 34 9 7
2 b z NaN 64 51 53
3 c z 7 23 87 66
否则,只需对前三列进行简单合并,而不进行排序即可:
result = df1.merge(df2, on=['col_1', 'col_2', 'col_3'], sort=False)
print(result)
输出:
col_1 col_2 col_3 col_4_x col_4_y col_5
0 a x NaN 54 14 14
1 a y 5 34 87 66
2 b z NaN 64 9 7
3 c z 7 23 51 53