pd.concat(objs, axis=0, join='outer', join_axes=none, ignore_index=false,keys=none, levels=none, names=none, verify_integrity=false)
objs:series或者dataframe物件構成的序列
asix:需要合併連線的軸,0是行,1是列
join:連線的方式,inner內連線,outer外連線
其它不常用引數......
#建立兩個物件
ser_1 = pd.series(np.random.randint(0,5,5),index=np.arange(5))
print(ser_1)
ser_2 = pd.series(np.random.randint(5,10,5),index=np.arange(5))
print(ser_2)
'''0 1
1 0
2 4
3 0
4 4
dtype: int32
0 7
1 9
2 6
3 5
4 5
dtype: int32
'''#axis預設為0,是橫向連線,返回乙個series物件
ser = pd.concat([ser_1,ser_2])
print(ser)
'''0 1
1 0
2 4
3 0
4 4
0 7
1 9
2 6
3 5
4 5
dtype: int32
'''
ser_1 = pd.series(np.random.randint(0,10,5),index=range(5))
ser_2 = pd.series(np.random.randint(0,10,4),index=range(4))
ser_3 = pd.series(np.random.randint(0,10,3),index=range(3))
print(ser_1)
'''0 4
1 3
2 6
3 4
4 6
dtype: int32
'''print(ser_2)
'''0 8
1 9
2 9
3 5
dtype: int32
'''print(ser_3)
'''0 6
1 0
2 3
dtype: int32
'''ser = pd.concat([ser_1,ser_2,ser_3],axis=1,join='inner')
print(ser)
'''dtype: int32
0 1 2
0 4 8 6
1 3 9 0
2 6 9 3
'''
ser_1 = pd.series(np.random.randint(0,10,5),index=range(5))
ser_2 = pd.series(np.random.randint(0,10,4),index=range(4))
ser_3 = pd.series(np.random.randint(0,10,3),index=range(3))
print(ser_1)
'''0 2
1 7
2 6
3 7
4 3
dtype: int32
'''print(ser_2)
'''0 7
1 4
2 7
3 0
dtype: int32
'''print(ser_3)
'''0 3
1 3
2 5
dtype: int32
'''ser = pd.concat([ser_1,ser_2,ser_3],axis=1,join='outer')
print(ser)
#返回乙個dataframe物件,並且取並集,對應位置沒有資料自動填充nan
''' 0 1 2
0 2 7.0 3.0
1 7 4.0 3.0
2 6 7.0 5.0
3 7 0.0 nan
4 3 nan nan
'''
# 建立兩個dataframe物件
df_1 = pd.dataframe(np.random.randint(0,10,(3,2)),index=['a','b','c'],columns=['a','b'])
df_2 = pd.dataframe(np.random.randint(0,10,(2,2)),index=['a','b'],columns=['c','d'])
print(df_1)
''' a b
a 7 0
b 0 8
c 9 3
'''print(df_2)
''' c d
a 7 5
b 4 0
'''df = pd.concat([df_1,df_2],axis=0)
print(df)
''' a b c d
a 9.0 9.0 nan nan
b 0.0 3.0 nan nan
c 4.0 7.0 nan nan
a nan nan 1.0 3.0
b nan nan 2.0 8.0
'''
# 建立兩個dataframe物件
df_1 = pd.dataframe(np.random.randint(0,10,(3,2)),index=['a','b','c'],columns=['a','b'])
df_2 = pd.dataframe(np.random.randint(0,10,(2,2)),index=['a','b'],columns=['c','d'])
print(df_1)
''' a b
a 5 6
b 8 9
c 4 7
'''print(df_2)
''' c d
a 5 4
b 2 0
'''df = pd.concat([df_1,df_2],axis=1)
print(df)
''' a b c d
a 5 6 5.0 4.0
b 8 9 2.0 0.0
c 4 7 nan nan
'''
import numpy as np
import pandas as pd
arr_1 = np.random.randint(0,10,(3,4))
arr_2 = np.random.randint(0,10,(3,4))
print(arr_1)
'''[[1 8 7 9]
[7 5 5 3]
[0 7 5 7]]
'''print(arr_2)
'''[[8 8 2 9]
[5 9 5 8]
[3 1 6 5]]
'''# concatenate 函式 合併的時候有軸向
arr = np.concatenate([arr_1,arr_2],axis=0)
print(arr)
'''[[1 8 7 9]
[7 5 5 3]
[0 7 5 7]
[8 8 2 9]
[5 9 5 8]
[3 1 6 5]]
'''arr = np.concatenate([arr_1,arr_2],axis=1)
print(arr)
'''[[1 8 7 9 8 8 2 9]
[7 5 5 3 5 9 5 8]
[0 7 5 7 3 1 6 5]]
'''
03 10Pandas 資料合併concat
注意concat與merge的區別,concat是沿軸方向將多個物件合併到一起。numpy 和 pandas裡都有實現concat的函式與功能。import numpy as np import pandas as pd 建立兩個dataframe arr1 np.random.randint 0,...
Pandas高階 合併資料集concat
本文主要介紹pandas中常用的資料合併的方法concat。先定義乙個生產資料的函式 乙個簡單的dataframe def make df cols,ind data return pd.dataframe data,ind 看下函式效果 in make df ab 1,2 out a b1 a1 ...
pandas資料合併與重塑(concat)
concat函式是在pandas底下的方法,可以將資料根據不同的軸作簡單的融合 pd.concat objs,axis 0,join outer join axes none ignore index false keys none levels none names none verify int...