6.Pandas的数据合并

0 引言

Pandas对多个表格可以进行合并操作,主要分为纵向合并和横向合并。

1 纵向合并

import pandas as pd
import numpy as np

生成多个DataFrame表格

df1 = pd.DataFrame(np.arange(12).reshape((3,4)),columns=['a','b','c','d'])
df2 = pd.DataFrame(np.arange(12,24).reshape((3,4)),columns=['a','b','c','d'])
df3 = pd.DataFrame(np.arange(24,36).reshape((3,4)),columns=['a','b','c','d'])
print(df1)
print(df2)
print(df3)
   a  b   c   d
0  0  1   2   3
1  4  5   6   7
2  8  9  10  11
    a   b   c   d
0  12  13  14  15
1  16  17  18  19
2  20  21  22  23
    a   b   c   d
0  24  25  26  27
1  28  29  30  31
2  32  33  34  35

调用 .concat 函数,axis=0表示列,即进行纵向合并

# 纵向合并
df4 = pd.concat([df1,df2,df3],axis=0)
df4
abcd
00123
14567
2891011
012131415
116171819
220212223
024252627
128293031
232333435

.concat 函数有个参数可以不考虑原来的索引 index

df4 = pd.concat([df1,df2,df3],axis=0,ignore_index=True) # 不考虑原来的index
df4
abcd
00123
14567
2891011
312131415
416171819
520212223
624252627
728293031
832333435

2 横向合并

调用 .concat 函数,参数axis=1表示行,也就是横向合并

# 横向合并
df5 = pd.concat([df1,df2,df3],axis=1)
df5
abcdabcdabcd
001231213141524252627
145671617181928293031
28910112021222332333435
df1 = pd.DataFrame(np.arange(12).reshape((3,4)),columns=['a','b','c','f'])
df2 = pd.DataFrame(np.arange(12,24).reshape((3,4)),columns=['a','c','d','e'])
print(df1)
print(df2)
   a  b   c   f
0  0  1   2   3
1  4  5   6   7
2  8  9  10  11
    a   c   d   e
0  12  13  14  15
1  16  17  18  19
2  20  21  22  23

.concat 函数中参数join设置为’outer’ 并且 ignore_index设置为True可以把表格缺少部分填充为NaN

df6 = pd.concat([df1,df2],join='outer',ignore_index=True) # 合并两个表,缺少的部分填充 NaN
df6
D:\Anaconda3\lib\site-packages\ipykernel_launcher.py:1: FutureWarning: Sorting because non-concatenation axis is not aligned. A future version
of pandas will change to not sort by default.

To accept the future behavior, pass 'sort=True'.

To retain the current behavior and silence the warning, pass sort=False

  """Entry point for launching an IPython kernel.
abcdef
001.02NaNNaN3.0
145.06NaNNaN7.0
289.010NaNNaN11.0
312NaN1314.015.0NaN
416NaN1718.019.0NaN
520NaN2122.023.0NaN

.concat 函数也可以实现只合并两个表相同索引 index 的部分

df6 = pd.concat([df1,df2],join='inner',ignore_index=True) # 只合并两个表相同的index部分,缺少的部分去掉
df6
ac
002
146
2810
31213
41617
52021
df1 = pd.DataFrame(np.arange(12).reshape((3,4)),columns=['a','b','c','f'])
df2 = pd.DataFrame(np.arange(12,24).reshape((4,3)),columns=['a','c','d'])
print(df1)
print(df2)
   a  b   c   f
0  0  1   2   3
1  4  5   6   7
2  8  9  10  11
    a   c   d
0  12  13  14
1  15  16  17
2  18  19  20
3  21  22  23

横向合并,并可以指定使用某个表格的索引 index

df8 = pd.concat([df1,df2],axis=1,join_axes=[df1.index]) # 横向合并,index使用df1的index
df8
abcfacd
00123121314
14567151617
2891011181920
df8 = pd.concat([df1,df2],axis=1) # 横向合并
df8
abcfacd
00.01.02.03.0121314
14.05.06.07.0151617
28.09.010.011.0181920
3NaNNaNNaNNaN212223
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZPILOTE

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值