pandas 合并/串联/合并多个数据帧(水平和垂直)
示例
生成样本数据帧:
In [57]: df3 = pd.DataFrame({'col1':[211,212,213], 'col2': [221,222,223]})
In [58]: df1 = pd.DataFrame({'col1':[11,12,13], 'col2': [21,22,23]})
In [59]: df2 = pd.DataFrame({'col1':[111,112,113], 'col2': [121,122,123]})
In [60]: df3 = pd.DataFrame({'col1':[211,212,213], 'col2': [221,222,223]})
In [61]: df1
Out[61]:
col1 col2
0 11 21
1 12 22
2 13 23
In [62]: df2
Out[62]:
col1 col2
0 111 121
1 112 122
2 113 123
In [63]: df3
Out[63]:
col1 col2
0 211 221
1 212 222
2 213 223垂直合并/合并/串联数据帧[df1,df2,df3]-添加行
In [64]: pd.concat([df1,df2,df3], ignore_index=True) Out[64]: col1 col2 0 11 21 1 12 22 2 13 23 3 111 121 4 112 122 5 113 123 6 211 221 7 212 222 8 213 223
水平合并/合并/串联数据帧(按索引对齐):
In [65]: pd.concat([df1,df2,df3], axis=1) Out[65]: col1 col2 col1 col2 col1 col2 0 11 21 111 121 211 221 1 12 22 112 122 212 222 2 13 23 113 123 213 223