# 连接和修补concat、combine_first 沿轴的堆叠连接 # 连接concat
import pandas as pd
import numpy as np s1 = pd.Series([1,2,3]) s2 = pd.Series([2,3,4]) s3 = pd.Series([1,2,3],index=[‘a‘,‘c‘,‘h‘]) s4 = pd.Series([2,3,4],index=[‘b‘,‘e‘,‘d‘]) print(s1) print(s2) print(pd.concat([s1,s2]))#直接堆接 print(pd.concat([s3,s4]).sort_index())#排序 print(‘-------‘) #默认axis = 0,行+行 print(pd.concat([s1,s2],axis=1)) #当axis=0时,列+列 成为dataframe
结果:
0 1 1 2 2 3 dtype: int64 0 2 1 3 2 4 dtype: int64 0 1 1 2 2 3 0 2 1 3 2 4 dtype: int64 a 1 b 2 c 2 d 4 e 3 h 3 dtype: int64 ------- 0 1 0 1 2 1 2 3 2 3 4
#连接方式 join join_axs s5 = pd.Series([1,2,4],index=[‘a‘,‘b‘,‘c‘]) s6 = pd.Series([2,3,4],index=[‘b‘,‘c‘,‘d‘]) print(s5) print(s6) print(pd.concat([s5,s6],axis=1)) print(pd.concat([s5,s6],axis=1,join=‘inner‘))#两边同时存在 print(pd.concat([s5,s6],axis=1,join_axes=[[‘a‘,‘b‘,‘c‘]]))#以index = [‘a‘,‘b‘,‘c‘]为基准去判断 #join_axes指定联合的index
结果:
a 1 b 2 c 4 dtype: int64 b 2 c 3 d 4 dtype: int64 0 1 a 1.0 NaN b 2.0 2.0 c 4.0 3.0 d NaN 4.0 0 1 b 2 2 c 4 3 0 1 a 1 NaN b 2 2.0 c 4 3.0
#层次索引 print(pd.concat([s5,s6],axis=1,keys=[‘one‘,‘two‘]))#覆盖列名 print(‘---‘) print(pd.concat([s5,s6],axis=0,keys=[‘one‘,‘two‘]))
结果: