如何使用多索引移动 Pandas DataFrame?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23198053/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 21:57:11  来源:igfitidea点击:

How do you shift Pandas DataFrame with a multiindex?

pythonpandas

提问by TravisVOX

With the following DataFrame, how can I shift the "beyer" column based on the index without having Pandas assign the shifted value to a different index value?

使用以下 DataFrame,如何在不让 Pandas 将移位值分配给不同索引值的情况下根据索引移动“beyer”列?

                  line_date  line_race  beyer
horse                                        
Last Gunfighter  2013-09-28         10     99
Last Gunfighter  2013-08-18         10    102
Last Gunfighter  2013-07-06          8    103
.....
Paynter          2013-09-28         10    103
Paynter          2013-08-31         10     88
Paynter          2013-07-27          8    100

df['beyer'].shift(1)produces...

df['beyer'].shift(1)产生...

                  line_date  line_race  beyer  beyer_shifted
horse                                                       
Last Gunfighter  2013-09-28         10     99            NaN
Last Gunfighter  2013-08-18         10    102             99
Last Gunfighter  2013-07-06          8    103            102
.....
Paynter          2013-09-28         10    103             71
Paynter          2013-08-31         10     88            103
Paynter          2013-07-27          8    100             88

The problem is that Paynter was given a beyer that Last Gunfighter (his first record) was assigned. Instead I want it to go like this...

问题是佩恩特获得了最后的枪手(他的第一个记录)被分配的拜尔。相反,我希望它像这样......

                  line_date  line_race  beyer  beyer_shifted
horse                                                       
Last Gunfighter  2013-09-28         10     99            NaN
Last Gunfighter  2013-08-18         10    102             99
Last Gunfighter  2013-07-06          8    103            102
.....
Paynter          2013-09-28         10    103            NaN
Paynter          2013-08-31         10     88            103
Paynter          2013-07-27          8    100             88

回答by unutbu

Use groupby/shiftto apply the shift to each group individually: (Thanks to Jeff for pointing out this simplification.)

使用groupby/shift应用转移到各组分别:(感谢Jeff指出这个简化)

In [60]: df['beyer_shifted'] = df.groupby(level=0)['beyer'].shift(1); df
Out[61]: 
                  line_date  line_race  beyer  beyer_shifted
Last Gunfighter  2013-09-28         10     99            NaN
Last Gunfighter  2013-08-18         10    102             99
Last Gunfighter  2013-07-06          8    103            102
Paynter          2013-09-28         10    103            NaN
Paynter          2013-08-31         10     88            103
Paynter          2013-07-27          8    100             88

If you have a multiindex, you can group by more than one level by passing a sequence of intsor level names to groupby'slevelparameter.

如果您有一个多索引,您可以通过将一系列ints或级别名称传递给groupby'slevel参数来按多个级别进行分组。