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


With the following dataframe, how can I move the "hier" column on the basis of the index without having to move values Different Index values?

  line_date line_resear bayer horse final gunfiller 2013-09-28 10 99 Final Gunfilder 2013-08-18 10 102 Ultimate Gunfitter 2013-07-06 8 103 .... Painter 2013-09- 28 10 103 painter 2013-08-31 10 88 painter 2013-07-27 8 100   

df ['beyer'] Shift (1) production ...

  line_date line_resayer bair_shiked horse final gunfighter 2013-09-28 10 99 Nain Ultimate Gunfilder 2013-08-18 10 102 99 Ultimate Gunfighter 2013 -07-06 8 103 102 ..... Painter 2013-09-28 10 103 71 Painters 2013-08-31 10 88 103 Painter 2013-07-27 8 100 88   

The problem is that the painter was given a bayer that the final ginfighter (his first record) was assigned. Instead, I want to go to it like this ...

  line_date line_resayer bair_shiked horse final gunfilter 2013-09-28 10 99 nan final gunfiller 2013-08-18 10 102 99 final Gunfighter 2013- 07-06 8 103 102 ..... Painter 2013-09-28 10 103 NN Painter 2013-08-31 10 88 103 Painter 2013-07-27 8 100 88   < / Div> 

Use groupby / shift to apply changes to each group individually: ( Thanks to Jeff for pointing to this simplification.) < / P>

  in [60]: df ['beyer_shifted'] = df.groupby (level = 0) ['beyer']. Shift (1); DF Out [61]: Line_Date Line_reserve Bear Bare_Shielded Last Gunfighter 2013-09-28 10 99 NAN Gun Gunfire 2013-08-18 10 102 99 Final Gunfire 2013-07-06 8 103 102 Painters 2013-09-28 10 103 NAN Painter 2013-08-31 10 88 103 Painter 2013-07-27 8 100 88 If you have a multi index, you can create groups at more than one level by going through the  sequence. Ints  or  groupby   level  parameter   

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -