pandas 重新索引多索引并按第二个索引移动值 [英] pandas reindex multiindex and shift values by second index

查看:42
本文介绍了 pandas 重新索引多索引并按第二个索引移动值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的 Pandas DataFrame :

I have a pandas DataFrame looking like this :

                        x1             x2            x3            x4
Date       Time                                                               
2017-01-03 09:00:00      0.000097      0.000259      0.000629      0.000142   
           09:20:00      0.000046      0.000044      0.000247      0.000134   
           09:40:00      0.000021      0.000032      0.000171      0.000105   
           10:00:00      0.000033      0.000040      0.000136      0.000178   
           10:20:00      0.000079      0.000157      0.000094      0.000083
           .....
           17:00:00      0.000032      0.000137      0.000024      0.000028

但是,我想通过一个 20 分钟的 bin 重新索引第二个索引,我希望它看起来像这样:

However, i want to reindex the second index, by one 20min bin and I would like it to look like this:

                        x1             x2            x3            x4
Date       Time                                                               
2017-01-03 09:20:00      0.000097      0.000259      0.000629      0.000142   
           09:40:00      0.000046      0.000044      0.000247      0.000134   
           10:00:00      0.000021      0.000032      0.000171      0.000105   
           10:20:00      0.000033      0.000040      0.000136      0.000178   
           10:40:00      0.000079      0.000157      0.000094      0.000083
           .....
           17:20:00      0.000032      0.000137      0.000024      0.000028

所以所有的值都保持不变,只有第二个索引被重命名,其他一切都保持不变.

So all the values stay the same, only the second index is renamed, everything else stays the same.

我尝试了以下代码:

x.reindex(pd.date_range(pd.Timestamp('09:20:00'), pd.Timestamp('17:20:00'), freq="20min").time, level=1)

但它只是移动索引并且值保持在同一位置.

But it just moves the index and the values stay at the same place.

                        x1             x2            x3            x4
Date       Time                                                               
2017-01-03 09:20:00      0.000046      0.000044      0.000247      0.000134   
           09:40:00      0.000021      0.000032      0.000171      0.000105   
           10:00:00      0.000033      0.000040      0.000136      0.000178   
           10:20:00      0.000079      0.000157      0.000094      0.000083
           .....
           17:00:00      0.000032      0.000137      0.000024      0.000028

它甚至没有在 17:20:00 投放垃圾箱.

It does not even ad the bin for 17:20:00.

但是,如果我还尝试在像这样分组后改变这些值:

However, if I also tried to shift the values after grouping them like this:

x.groupby(level=1).shift(1)

或:

x.groupby(level=1).shift(1, freq='20min')

但这根本不起作用.

推荐答案

我能想到的最快方法是用它自己的 20 分钟移位版本覆盖 MultiIndex 的整个第一级(最内级):

The fastest way I can think of is to overwrite the entire first level (innermost level) of the MultiIndex with a 20-minute-shifted version of itself:

x.index = x.index.set_levels(x.index.levels[1].shift(20, 'min'), level=1)

示例

x = pd.DataFrame(index=pd.MultiIndex.from_product([pd.date_range('2017-01-03', '2017-01-06', freq='1D'), 
                                                   pd.date_range('09:00', '17:00', freq='20min')]))
x.loc[:, 'x1'] = list(range(len(x)))

x
                                x1
2017-01-03 2018-06-14 09:00:00   0
           2018-06-14 09:20:00   1
           2018-06-14 09:40:00   2
           2018-06-14 10:00:00   3
           2018-06-14 10:20:00   4
    ...                         ..
2017-01-06 2018-06-14 15:40:00  95
           2018-06-14 16:00:00  96
           2018-06-14 16:20:00  97
           2018-06-14 16:40:00  98
           2018-06-14 17:00:00  99

x.index = x.index.set_levels(x.index.levels[1].shift(20, 'min'), level=1)

x
                                x1
2017-01-03 2018-06-14 09:20:00   0
           2018-06-14 09:40:00   1
           2018-06-14 10:00:00   2
           2018-06-14 10:20:00   3
           2018-06-14 10:40:00   4
    ...                         ..
2017-01-06 2018-06-14 16:00:00  95
           2018-06-14 16:20:00  96
           2018-06-14 16:40:00  97
           2018-06-14 17:00:00  98
           2018-06-14 17:20:00  99

这篇关于 pandas 重新索引多索引并按第二个索引移动值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆