Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
494 views
in Technique[技术] by (71.8m points)

python - How do you shift Pandas DataFrame with a multiindex?

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?

                  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...

                  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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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 ints or level names to groupby's level parameter.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...