-
pd.concat(objs, axis= 0, join='outer', join_axes=None, ignore_index=False,
-
keys= None, levels=None, names=None, verify_integrity=False,
-
copy= True)
參數含義
objs
:Series,DataFrame或Panel對象的序列或映射。如果傳遞了dict,則排序的鍵將用作鍵參數,除非它被傳遞,在這種情況下,將選擇值(見下文)。任何無對象將被靜默刪除,除非它們都是無,在這種情況下將引發一個ValueError。axis
:{0,1,...},默認為0。沿着連接的軸。join
:{'inner','outer'},默認為“outer”。如何處理其他軸上的索引。outer為聯合和inner為交集。ignore_index
:boolean,default False。如果為True,請不要使用並置軸上的索引值。結果軸將被標記為0,...,n-1。如果要連接其中並置軸沒有有意義的索引信息的對象,這將非常有用。注意,其他軸上的索引值在連接中仍然受到尊重。join_axes
:Index對象列表。用於其他n-1軸的特定索引,而不是執行內部/外部設置邏輯。keys
:序列,默認值無。使用傳遞的鍵作為最外層構建層次索引。如果為多索引,應該使用元組。levels
:序列列表,默認值無。用於構建MultiIndex的特定級別(唯一值)。否則,它們將從鍵推斷。names
:list,default無。結果層次索引中的級別的名稱。verify_integrity
:boolean,default False。檢查新連接的軸是否包含重復項。這相對於實際的數據串聯可能是非常昂貴的。copy
:boolean,default True。如果為False,請勿不必要地復制數據。
import numpy as np import pandas as pd from pandas import Series,DataFrame
一、Numpy數組的連接–concatenate
a = np.arange(6).reshape(2,3) a
array([[0, 1, 2],
[3, 4, 5]])
1.垂直連接
np.concatenate([a,a])
array([[0, 1, 2],
[3, 4, 5],
[0, 1, 2],
[3, 4, 5]])
2.水平連接
np.concatenate([a,a],axis=1)
array([[0, 1, 2, 0, 1, 2],
[3, 4, 5, 3, 4, 5]])
二、pandas.concat函數
s1 = Series([0,1],index = ['a','b']) s2 = Series([2,3,4],index = ['c','d','e']) s3 = Series([5,6],index = ['f','g'])
1.垂直連接
pd.concat([s1,s2,s3])
a 0
b 1
c 2
d 3
e 4
f 5
g 6
dtype: int64
2.水平連接
print(pd.concat([s1,s2,s3],axis=1))
0 1 2
a 0.0 NaN NaN
b 1.0 NaN NaN
c NaN 2.0 NaN
d NaN 3.0 NaN
e NaN 4.0 NaN
f NaN NaN 5.0
g NaN NaN 6.0
3.對連接的對象添加索引(實現層次索引)
print(pd.concat([s1,s2,s3],keys=['one','two','thress']))
one a 0
b 1
two c 2
d 3
e 4
thress f 5
g 6
dtype: int64
4.水平連接時,為結果的DataFrame添加列索引
print(pd.concat([s1,s2,s3],axis=1,keys=['one','two','three']))
one two three
a 0.0 NaN NaN
b 1.0 NaN NaN
c NaN 2.0 NaN
d NaN 3.0 NaN
e NaN 4.0 NaN
f NaN NaN 5.0
g NaN NaN 6.0