【问题标题】:Merge two Series according to their index根据索引合并两个系列
【发布时间】:2020-04-14 21:01:59
【问题描述】:

在谷歌上搜索了很长时间,但没有找到解决我可能经常被问到的问题的方法。

我有两个数据框:

DF1:             DF2:
       val                 val
index            index      
  1      3        2         5
  3     10        4         15
  5     20        7         35
  6     30        8         40

并且需要这样的输出:

DF_out:
       val 
index
  1      3 
  2      5 
  3      10 
  4      15
  5      20
  6      30 
  7      35 
  8      40

DF1 和 DF2 应该根据它们的索引组合和排序。 旁注:

  • DF1 和 DF2 永远不会有两次相同的索引
  • 数据帧的值总是后续的

非常感谢您的帮助!

【问题讨论】:

标签: python pandas


【解决方案1】:

concatSeries.sort_index 一起使用:

df = pd.concat([DF1, DF2]).sort_index()
print (df)
       val
index     
1        3
2        5
3       10
4       15
5       20
6       30
7       35
8       40

【讨论】:

    猜你喜欢
    • 2015-10-28
    • 2022-01-12
    • 2021-09-19
    • 2012-02-11
    • 2019-05-31
    • 2013-06-07
    • 2020-05-03
    • 2023-03-09
    • 2017-07-07
    相关资源
    最近更新 更多