追加到Multiindex pandas DataFrame中的级别

时间:2018-09-11 21:37:21

标签: python pandas multi-index

我的Multiindex数据框的结构如下:

                                  close       high        low       open  
   index = (timestamp,key)                                  
(2018-09-10 16:00:00, ask)       1.16023    1.16064    1.16007    1.16046
(2018-09-10 16:00:00, bid)       1.16009    1.16053    1.15992    1.16033
(2018-09-10 16:00:00, volume)  817.00000  817.00000  817.00000  817.00000

每个时间戳都有出价,要价和交易量的观察法。

我正在尝试通过计算相应的(bid + ask)/ 2,将“中间”观察值添加到索引的第二级(即[bid,ask,volume])。

我所需的数据框应如下所示

                                  close       high        low       open  
   index = (timestamp,key)                                  
(2018-09-10 16:00:00, ask)       1.16023    1.16064    1.16007    1.16046
(2018-09-10 16:00:00, bid)       1.16009    1.16053    1.15992    1.16033
(2018-09-10 16:00:00, volume)  817.00000  817.00000  817.00000  817.00000
(2018-09-10 16:00:00, mid)     1.16016      1.16059    1.15999    1.1604

最有效的方法是什么?能做到这一点吗?

编辑:

打印出数据框的头部以更清楚地看到结构。

`bid_ask.head(5).to_dict()
Out[3]: 
{'close': {(Timestamp('2018-09-10 16:00:00'), 'ask'): 1.1602300000000001,
  (Timestamp('2018-09-10 16:00:00'), 'bid'): 1.1600900000000001,
  (Timestamp('2018-09-10 16:00:00'), 'volume'): 817.0,
  (Timestamp('2018-09-10 17:00:00'), 'ask'): 1.15977,
  (Timestamp('2018-09-10 17:00:00'), 'bid'): 1.15968},
 'high': {(Timestamp('2018-09-10 16:00:00'), 'ask'): 1.1606399999999999,
  (Timestamp('2018-09-10 16:00:00'), 'bid'): 1.1605300000000001,
  (Timestamp('2018-09-10 16:00:00'), 'volume'): 817.0,
  (Timestamp('2018-09-10 17:00:00'), 'ask'): 1.16039,
  (Timestamp('2018-09-10 17:00:00'), 'bid'): 1.16029},
 'low': {(Timestamp('2018-09-10 16:00:00'), 'ask'): 1.1600699999999999,
  (Timestamp('2018-09-10 16:00:00'), 'bid'): 1.1599200000000001,
  (Timestamp('2018-09-10 16:00:00'), 'volume'): 817.0,
  (Timestamp('2018-09-10 17:00:00'), 'ask'): 1.1596200000000001,
  (Timestamp('2018-09-10 17:00:00'), 'bid'): 1.1595299999999999},
 'open': {(Timestamp('2018-09-10 16:00:00'), 'ask'): 1.16046,
  (Timestamp('2018-09-10 16:00:00'), 'bid'): 1.1603300000000001,
  (Timestamp('2018-09-10 16:00:00'), 'volume'): 817.0,
  (Timestamp('2018-09-10 17:00:00'), 'ask'): 1.1601900000000001,
  (Timestamp('2018-09-10 17:00:00'), 'bid'): 1.1600999999999999}}
 `

1 个答案:

答案 0 :(得分:0)

我不确定您的DataFrame的结构如何,但这是本质

df.loc[('2018-09-10 16:00:00', 'mid'), :] = [1.16016, 1.16059, 1.15999 , 1.1604]

您需要做的就是使用df.loc并为MultiIndex

提供一个 new 元组

我猜我以为您的新MultiIndex条目是('2018-09-10 16:00:00', 'mid')

示例

In [353]: ref

Out[353]:
       Names  Values
  idx2
1 one      A       5
2 two      B      10

In [354]: ref.loc[(3, 'three'), :] = ['C', 15]

In [355]: ref
Out[355]:
        Names  Values
  idx2
1 one       A     5.0
2 two       B    10.0
3 three     C    15.0