如何使用pd.concat连接df1和df2以获取df3?
df1=pd.DataFrame(data=[[1,2],[3,4],[5,6]],index=[1,2,2])
df2=pd.DataFrame(data=[[1,2],[3,4]],index=[2,2])
df3 =pd.DataFrame(data=[[1,2,np.nan,np.nan],[3,4,1,2],[5,6,3,4]],index=[1,2,2])
df1
Out[27]:
0 1
1 1 2
2 3 4
2 5 6
df2
Out[33]:
0 1
2 1 2
2 3 4
df3
Out[32]:
0 1 2 3
1 1 2 NaN NaN
2 3 4 1.0 2.0
2 5 6 3.0 4.0
答案 0 :(得分:0)
按cumcount
+ set_index
创建MultiIndex
,然后可以使用concat
:
df1 = df1.set_index(df1.groupby(level=0).cumcount(), append=True)
df2 = df2.set_index(df2.groupby(level=0).cumcount(), append=True)
df = pd.concat([df1, df2], axis=1, ignore_index=True).reset_index(drop=True, level=1)
print (df)
0 1 2 3
1 1 2 NaN NaN
2 3 4 1.0 2.0
2 5 6 3.0 4.0