如何在Python中合并完全不同的数据框架UP-SIDE。

问题描述 投票:0回答:1

这些是我的数据框架。

这些是不同的CSV文件,我想把它合并成一个CSV。

这可能吗?

df1

a b b1 c d
1 1  2 3 3
1 2  1 3 4
5 5  5 6 4

df2

r t y 
3 5 6 
6 2 1


i want to merge this horizontally

expected output-:

a b b1 c d
1 1  2 3 3
1 2  1 3 4
5 5  5 6 4

r t y 
3 5 6 
6 2 1


我用了 pd.mergepd.join 但我是不能够做到这一点谢谢

python pandas
1个回答
1
投票

试试这个。

df1 = df1.T.reset_index().T
df2 = df2.T.reset_index().T

df = pd.concat([df1, df2], axis=0).fillna("").reset_index(drop=True)

df.columns = df.loc[0,:]
df.drop(labels=0, inplace=True)

输出。

0  a  b b1  c  d
1  1  1  2  3  3
2  1  2  1  3  4
3  5  5  5  6  4
4  r  t  y      
5  3  5  6      
6  6  2  1      
© www.soinside.com 2019 - 2024. All rights reserved.