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
a b c d
0 0 1 2 3
1 4 5 6 7
2 8 9 10 11
0 12 13 14 15
1 16 17 18 19
2 20 21 22 23
0 24 25 26 27
1 28 29 30 31
2 32 33 34 35

.concat 函數有個參數可以不考慮原來的索引 index

df4 = pd.concat([df1,df2,df3],axis=0,ignore_index=True) # 不考慮原來的index
df4
a b c d
0 0 1 2 3
1 4 5 6 7
2 8 9 10 11
3 12 13 14 15
4 16 17 18 19
5 20 21 22 23
6 24 25 26 27
7 28 29 30 31
8 32 33 34 35

2 橫向合併

調用 .concat 函數,參數axis=1表示行,也就是橫向合併

# 橫向合併
df5 = pd.concat([df1,df2,df3],axis=1)
df5
a b c d a b c d a b c d
0 0 1 2 3 12 13 14 15 24 25 26 27
1 4 5 6 7 16 17 18 19 28 29 30 31
2 8 9 10 11 20 21 22 23 32 33 34 35
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.
a b c d e f
0 0 1.0 2 NaN NaN 3.0
1 4 5.0 6 NaN NaN 7.0
2 8 9.0 10 NaN NaN 11.0
3 12 NaN 13 14.0 15.0 NaN
4 16 NaN 17 18.0 19.0 NaN
5 20 NaN 21 22.0 23.0 NaN

.concat 函數也可以實現只合並兩個表相同索引 index 的部分

df6 = pd.concat([df1,df2],join='inner',ignore_index=True) # 只合並兩個表相同的index部分,缺少的部分去掉
df6
a c
0 0 2
1 4 6
2 8 10
3 12 13
4 16 17
5 20 21
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
a b c f a c d
0 0 1 2 3 12 13 14
1 4 5 6 7 15 16 17
2 8 9 10 11 18 19 20
df8 = pd.concat([df1,df2],axis=1) # 橫向合併
df8
a b c f a c d
0 0.0 1.0 2.0 3.0 12 13 14
1 4.0 5.0 6.0 7.0 15 16 17
2 8.0 9.0 10.0 11.0 18 19 20
3 NaN NaN NaN NaN 21 22 23
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章