【问题标题】:Merging Pandas Dataframe to Multiindex with Date使用日期将 Pandas 数据框合并到多索引
【发布时间】:2018-05-23 18:57:17
【问题描述】:

我有一些数据帧,其中包含来自多个来源的日期索引,我想将它们合并到一个多索引数据帧中。我正在努力弄清楚如何做到这一点。

从两个数据框开始:

来源 1

+---------------------+------+------+-----+-------+
|        date         | open | high | low | close |
+---------------------+------+------+-----+-------+
| 2018-04-04 20:00:00 | xxx  | xxx  | xxx | xxx   |
| 2018-04-04 21:00:00 | xxx  | xxx  | xxx | xxx   |
| 2018-04-04 22:00:00 | xxx  | xxx  | xxx | xxx   |
+---------------------+------+------+-----+-------+

来源 2

+---------------------+------+------+-----+-------+
|        date         | open | high | low | close |
+---------------------+------+------+-----+-------+
| 2018-04-04 20:00:00 | xxx  | xxx  | xxx | xxx   |
| 2018-04-04 21:00:00 | xxx  | xxx  | xxx | xxx   |
| 2018-04-04 22:00:00 | xxx  | xxx  | xxx | xxx   |
+---------------------+------+------+-----+-------+

我想将它们合并,以便它们在具有 source1 或 source2 的日期被多索引。

类似:

+---------------------+---------+------+-----+-------+
|                     |         |      |     |       |
+---------------------+---------+------+-----+-------+
| 2018-04-04 20:00:00 | source1 |      |     |       |
|                     | open    | high | low | close |
|                     | xxx     | xxx  | xxx | xxx   |
|                     | source2 |      |     |       |
|                     | open    | high | low | close |
|                     | xxx     | xxx  | xxx | xxx   |
| 2018-04-04 21:00:00 | source1 |      |     |       |
|                     | open    | high | low | close |
|                     | xxx     | xxx  | xxx | xxx   |
|                     | source2 |      |     |       |
|                     | open    | high | low | close |
|                     | xxx     | xxx  | xxx | xxx   |
| 2018-04-04 22:00:00 | source1 |      |     |       |
|                     | open    | high | low | close |
|                     | xxx     | xxx  | xxx | xxx   |
|                     | source2 |      |     |       |
|                     | open    | high | low | close |
|                     | xxx     | xxx  | xxx | xxx   |
+---------------------+---------+------+-----+-------+

谁能帮忙?

谢谢!

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    concatkeys 一起使用,将set_indexDatetimeIndex 一起使用,然后将swaplevelsort_index 一起使用:

    df = (pd.concat([df1.set_index('date'),df2.set_index('date')], keys=['source1','source2'])
            .swaplevel(0,1)
            .sort_index())
    print (df)
                                open high  low close
    date                                            
    2018-04-04 20:00:00 source1  xxx  xxx  xxx   xxx
                        source2  xxx  xxx  xxx   xxx
    2018-04-04 21:00:00 source1  xxx  xxx  xxx   xxx
                        source2  xxx  xxx  xxx   xxx
    2018-04-04 22:00:00 source1  xxx  xxx  xxx   xxx
                        source2  xxx  xxx  xxx   xxx
    

    【讨论】:

      【解决方案2】:

      你可以去concat指定键,即

      df3 = pd.concat([df1,df2],keys=['source1','source2']).reset_index(level=0)
      
      df3 = df3.set_index(['date','level_0']).sort_index(level='date')
      
      
      
                                      open    high    low    close
       date                 level_0                                
       2018-04-04 20:00:00  source1   xxx     xxx     xxx    xxx   
                            source2   xxx     xxx     xxx    xxx   
       2018-04-04 21:00:00  source1   xxx     xxx     xxx    xxx   
                            source2   xxx     xxx     xxx    xxx   
       2018-04-04 22:00:00  source1   xxx     xxx     xxx    xxx   
                            source2   xxx     xxx     xxx    xxx   
      

      【讨论】:

      • 谢谢!我确信我可以让这个解决方案发挥作用,但我只能批准一个答案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-11
      • 2012-12-01
      • 2021-01-10
      • 2021-11-29
      • 2014-06-10
      • 2020-01-27
      相关资源
      最近更新 更多