Pandas_规整数据_轴向连接_concat

轴向连接

pd.concat()是顶级的类方法,实例对象没有该方法。

参数类型说明
axis0 10 默认值
joinouter 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_indexbool

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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值