当前位置:   article > 正文

pandas数据合并_series合并

series合并

目录

1.merge数据合并

1.1merge默认合并数据

1.2左连接和右连接

1.3 merge通过多个键合并

2.concat数据连接

2.1两个Series的数据连接

2.2两个DataFrame的数据连接

3.combine_first合并数据


1.merge数据合并

  1. import pandas as pd
  2. import numpy as np

1.1merge默认合并数据

merge合并时默认是内连接(inner)

  1. price = pd.DataFrame({'fruit':['apple','grape','orange','orange'],'price':[8,7,9,11]})
  2. amount = pd.DataFrame({'fruit':['apple','grape','orange'],'amout':[5,11,8]})
  3. display(price,amount,pd.merge(price,amount))
  4. #------------------------------------------------------------------------
  5. fruit price
  6. 0 apple 8
  7. 1 grape 7
  8. 2 orange 9
  9. 3 orange 11
  10. fruit amout
  11. 0 apple 5
  12. 1 grape 11
  13. 2 orange 8
  14. fruit price amout
  15. 0 apple 8 5
  16. 1 grape 7 11
  17. 2 orange 9 8
  18. 3 orange 11 8

1.2左连接和右连接

  1. pd.merge(price,amount,how = 'left')#左连接
  2. pd.merge(price,amount,how = 'right'#右连接

1.3 merge通过多个键合并

  1. left = pd.DataFrame({'key1':['one','one','two'],'key2':['a','b','a'],'value1':range(3)})
  2. right = pd.DataFrame({'key1':['one','one','two','two'],'key2':['a','a','a','b'],'value2':range(4)})
  3. display(left,right,pd.merge(left,right,on = ['key1','key2'],how = 'left'))
  4. #--------------------------------------------------------------------------------
  5. key1 key2 value1
  6. 0 one a 0
  7. 1 one b 1
  8. 2 two a 2
  9. key1 key2 value2
  10. 0 one a 0
  11. 1 one a 1
  12. 2 two a 2
  13. 3 two b 3
  14. key1 key2 value1 value2
  15. 0 one a 0 0.0
  16. 1 one a 0 1.0
  17. 2 one b 1 NaN
  18. 3 two a 2 2.0

2.concat数据连接

concat方法默认情况下会按行的方向堆叠数据,如果在列向上连接,设置axis=1。

2.1两个Series的数据连接

  1. s1 = pd.Series([0,1],index = ['a','b'])
  2. s2 = pd.Series([2,3,4],index = ['a','d','e'])
  3. s3 = pd.Series([5,6],index = ['f','g'])
  4. print(pd.concat([s1,s2,s3])) #Series行合并
  5. #----------------------------------------------
  6. a 0
  7. b 1
  8. a 2
  9. d 3
  10. e 4
  11. f 5
  12. g 6

2.2两个DataFrame的数据连接

  1. data1 = pd.DataFrame(np.arange(6).reshape(2,3),columns = list('abc'))
  2. data2 = pd.DataFrame(np.arange(20,26).reshape(2,3),columns = list('ayz'))
  3. data = pd.concat([data1,data2],axis = 0,sort=False)
  4. display(data1,data2,data)
  5. #--------------------------------------------------------------------------
  6. a b c
  7. 0 0 1 2
  8. 1 3 4 5
  9. a y z
  10. 0 20 21 22
  11. 1 23 24 25
  12. a b c y z
  13. 0 0 1.0 2.0 NaN NaN
  14. 1 3 4.0 5.0 NaN NaN
  15. 0 20 NaN NaN 21.0 22.0
  16. 1 23 NaN NaN 24.0 25.0

3.combine_first合并数据

如果需要合并的两个DataFrame存在重复索引,可以使用combine_first方法。

  1. s6.combine_first(s5)
  2. #-----------------------
  3. 0 1
  4. a 0.0 0.0
  5. b 1.0 5.0
  6. f NaN 5.0
  7. g NaN 6.0

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/663904
推荐阅读
相关标签
  

闽ICP备14008679号