轴向连接
pd.concat()是顶级的类方法,实例对象没有该方法。
参数 | 类型 | 说明 |
axis | 0 1 | 0 默认值 |
join | outer inner | outer 默认值 inner 交集,outer 并集 axis=0,依据columns取并集或交集(取交集时,columns相同的数据保留) axis=1,依据index取交集或并集(取交集时,index相同的数据保留) |
join_axes | 索引组成的列表 | pd.concat( [df1,df2],join_axes=[df1.columns] ) 0轴方向取并集,且只保留df1的columns |
ignore_index | bool | False 默认值 True 忽略连接轴方向的索引,建立新的整数索引( 0,1...n-1 ) axis=0,0轴方向索引重置 axis=1,1轴方向索引重置 |
keys | 新增加的索引 axis=0,在0轴方向设置多级索引 axis=1,在1轴方向设置多级索引 | |
names | 列表 | 设置每一级索引的名称 |
levels |
df1
one two three
a 110 120 130
c 210 220 230
d 310 320 330
df2
one three five
b 11 12 13
c 21 22 23
d 31 32 33
pd.concat([df1,df2],axis=1)
one two three one three five
a 110.0 120.0 130.0 NaN NaN NaN
b NaN NaN NaN 11.0 12.0 13.0
c 210.0 220.0 230.0 21.0 22.0 23.0
d 310.0 320.0 330.0 31.0 32.0 33.0
pd.concat([df1,df2],axis=1,join='inner')
one two three one three five
c 210 220 230 21 22 23
d 310 320 330 31 32 33
pd.concat([df1,df2],keys=['level1','level2'],names=['LEVEL','ROW'])
five one three two
LEVEL ROW
level1 a NaN 110 130 120.0
c NaN 210 230 220.0
d NaN 310 330 320.0
level2 b 13.0 11 12 NaN
c 23.0 21 22 NaN
d 33.0 31 32 NaN
pd.concat([df1,df2],axis=1,keys=['level1','level2'],names=['LEVEL','ROW'])
LEVEL level1 level2
ROW one two three one three five
a 110.0 120.0 130.0 NaN NaN NaN
b NaN NaN NaN 11.0 12.0 13.0
c 210.0 220.0 230.0 21.0 22.0 23.0
d 310.0 320.0 330.0 31.0 32.0 33.0
pd.concat([df1,df2],join_axes=[df1.columns])
one two three
a 110 120.0 130
c 210 220.0 230
d 310 320.0 330
b 11 NaN 12
c 21 NaN 22
d 31 NaN 32