python--pandas合并

#concatenating
df1 = pd.DataFrame(np.ones((3,4))*0,columns = ['A','B','C','D'])
df2 = pd.DataFrame(np.ones((3,4))*1,columns = ['A','B','C','D'])
df3 = pd.DataFrame(np.ones((3,4))*2,columns = ['A','B','C','D'])
print(df1,df2,df3,'#####')
res = pd.concat([df1,df2,df3],axis = 0)
print(res)
res = pd.concat([df1,df2,df3],axis = 0,ignore_index=True)#忽略索引号,重新统一加索引   
print(res)

# join,['inner','outer']默认值是outer

df1 = pd.DataFrame(np.ones((3,4))*0,columns = ['A','B','C','D'],index = [1,2,3])
df2 = pd.DataFrame(np.ones((3,4))*1,columns = ['B','C','D','E'],index = [2,3,4])#部分重合
print(df1)
print(df2)
res = pd.concat([df1,df2])#outer
print(res)
res = pd.concat([df1,df2],join = 'inner')#合并重叠的部分
print(res)

#join_axes
df1 = pd.DataFrame(np.ones((3,4))*0,columns = ['A','B','C','D'],index = [1,2,3])
df2 = pd.DataFrame(np.ones((3,4))*1,columns = ['B','C','D','E'],index = [2,3,4])#部分重合

res = pd.concat([df1,df2],axis=1,join_axes= [df1.index])#按df1的索引合并
print(res)

#append 默认是加行,竖向加
df1 = pd.DataFrame(np.ones((3,4))*0,columns = ['A','B','C','D'])
df2 = pd.DataFrame(np.ones((3,4))*1,columns = ['A','B','C','D'])

res = df1.append(df2)
print(res)
s1 = pd.Series([1,2,3,4],index =['A','B','C','D'])
res=df1.append(s1,ignore_index=True)
print(res)

结果显示:

A    B    C    D
0  0.0  0.0  0.0  0.0
1  0.0  0.0  0.0  0.0
2  0.0  0.0  0.0  0.0      A    B    C    D
0  1.0  1.0  1.0  1.0
1  1.0  1.0  1.0  1.0
2  1.0  1.0  1.0  1.0      A    B    C    D
0  2.0  2.0  2.0  2.0
1  2.0  2.0  2.0  2.0
2  2.0  2.0  2.0  2.0 #####
     A    B    C    D
0  0.0  0.0  0.0  0.0
1  0.0  0.0  0.0  0.0
2  0.0  0.0  0.0  0.0
0  1.0  1.0  1.0  1.0
1  1.0  1.0  1.0  1.0
2  1.0  1.0  1.0  1.0
0  2.0  2.0  2.0  2.0
1  2.0  2.0  2.0  2.0
2  2.0  2.0  2.0  2.0
     A    B    C    D
0  0.0  0.0  0.0  0.0
1  0.0  0.0  0.0  0.0
2  0.0  0.0  0.0  0.0
3  1.0  1.0  1.0  1.0
4  1.0  1.0  1.0  1.0
5  1.0  1.0  1.0  1.0
6  2.0  2.0  2.0  2.0
7  2.0  2.0  2.0  2.0
8  2.0  2.0  2.0  2.0
     A    B    C    D
1  0.0  0.0  0.0  0.0
2  0.0  0.0  0.0  0.0
3  0.0  0.0  0.0  0.0
     B    C    D    E
2  1.0  1.0  1.0  1.0
3  1.0  1.0  1.0  1.0
4  1.0  1.0  1.0  1.0
     A    B    C    D    E
1  0.0  0.0  0.0  0.0  NaN
2  0.0  0.0  0.0  0.0  NaN
3  0.0  0.0  0.0  0.0  NaN
2  NaN  1.0  1.0  1.0  1.0
3  NaN  1.0  1.0  1.0  1.0
4  NaN  1.0  1.0  1.0  1.0
     B    C    D
1  0.0  0.0  0.0
2  0.0  0.0  0.0
3  0.0  0.0  0.0
2  1.0  1.0  1.0
3  1.0  1.0  1.0
4  1.0  1.0  1.0
     A    B    C    D    B    C    D    E
1  0.0  0.0  0.0  0.0  NaN  NaN  NaN  NaN
2  0.0  0.0  0.0  0.0  1.0  1.0  1.0  1.0
3  0.0  0.0  0.0  0.0  1.0  1.0  1.0  1.0
     A    B    C    D
0  0.0  0.0  0.0  0.0
1  0.0  0.0  0.0  0.0
2  0.0  0.0  0.0  0.0
0  1.0  1.0  1.0  1.0
1  1.0  1.0  1.0  1.0
2  1.0  1.0  1.0  1.0
     A    B    C    D
0  0.0  0.0  0.0  0.0
1  0.0  0.0  0.0  0.0
2  0.0  0.0  0.0  0.0
3  1.0  2.0  3.0  4.0
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值