合并具有相同列的两个相似的数据帧

2024-04-19 09:10:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我想合并df_1和df_2来创建df_merged,但是我想合并两个有共同点的列,而不是像创建了一个或多个这样的列。在

index = [np.array(['foo', 'foo', 'qux', 'qux']),
         np.array(['one', 'two', 'one', 'two',])]
columns = ["A",  "B"]
df_1 = pd.DataFrame(np.random.randn(4, 2), index=index, columns=columns)

index = [np.array(['bar', 'bar', 'baz', 'baz',]),
         np.array(['one', 'two', 'one', 'two',])]
columns = ["A",  "B"]
df_2 = pd.DataFrame(np.random.randn(4, 2), index=index, columns=columns)

df_merge = pd.merge(df_1, df_2, left_index=True, right_index=True, how='outer')

print df_1
print df_2
print df_merge

df_1

^{pr2}$

df_2

                A         B
bar one  0.134571  0.415209
    two -1.288889 -0.144284
baz one -0.117345 -0.095292
    two -0.256708 -0.682542

df_merge-电流输出

              A_x       B_x       A_y       B_y
bar one       NaN       NaN  0.134571  0.415209
    two       NaN       NaN -1.288889 -0.144284
baz one       NaN       NaN -0.117345 -0.095292
    two       NaN       NaN -0.256708 -0.682542
foo one  2.082229  1.575985       NaN       NaN
    two -0.805592  0.444195       NaN       NaN
qux one  0.368874  0.253556       NaN       NaN
    two  1.017632 -0.471978       NaN       NaN

df_merge-需要

              A         B       
bar one        0.134571  0.415209
    two       -1.288889 -0.144284
baz one       -0.117345 -0.095292
    two       -0.256708 -0.682542
foo one        2.082229  1.575985       
    two       -0.805592  0.444195       
qux one        0.368874  0.253556      
    two        1.017632 -0.471978       

Tags: columnsdfindexfoonpbarbazmerge
1条回答
网友
1楼 · 发布于 2024-04-19 09:10:58

最简单的方法是使用^{},默认情况下,'outer'沿着特定的轴连接pandas对象(这里是axis=0,默认值):

print (pd.concat([df_1,df_2]))

                A         B
foo one -0.329887 -0.966898
    two  0.552272 -1.964264
qux one -0.629764 -0.765578
    two -0.148118  0.904920
bar one  0.305685 -1.269400
    two  1.256213 -0.686447
baz one -2.194461  0.529666
    two -1.487217 -0.520045

然后^{}如果需要:

^{pr2}$

相关问题 更多 >