2018.03.27 pandas concat 和 combin_first使用

# 连接和修补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']))

结果:

   one  two
a  1.0  NaN
b  2.0  2.0
c  4.0  3.0
d  NaN  4.0
---
one  a    1
     b    2
     c    4
two  b    2
     c    3
     d    4
dtype: int64
#修补
df1 = pd.DataFrame([[np.nan,3,5],[-1,6,np.nan],[np.nan,7,np.nan]])
df2 = pd.DataFrame([[-42.6,np.nan,-8.2],[-5,1.6,4]],index=[1,2])
print(df1)
print(df2)
print(df1.combine_first(df2))#按照索引使用df2中的值填补df1z中缺失的内容
#根据index df1的空值被df2代替
#如果df2的index多余df1,则更新到df1上,比如index=['a',1]

df1.update(df2)
print(df1)
#直接用df2的值覆盖df1

结果:

     0  1    2
0  NaN  3  5.0
1 -1.0  6  NaN
2  NaN  7  NaN
      0    1    2
1 -42.6  NaN -8.2
2  -5.0  1.6  4.0
     0  1    2
0  NaN  3  5.0
1 -1.0  6 -8.2
2 -5.0  7  4.0
      0    1    2
0   NaN  3.0  5.0
1 -42.6  6.0 -8.2
2  -5.0  1.6  4.0
posted @ 2018-03-27 17:33  TAB_Zhu  阅读(252)  评论(0编辑  收藏  举报