Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you shift Pandas DataFrame with a multiindex?

Tags:

python

pandas

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 
like image 309
TravisVOX Avatar asked Apr 21 '14 13:04

TravisVOX


People also ask

What is the best way to shift a Pandas DataFrame column?

shift() If you want to shift your column or subtract the column value with the previous row value from the DataFrame, you can do it by using the shift() function. It consists of a scalar parameter called period, which is responsible for showing the number of shifts to be made over the desired axis.

How do you shift data frames?

shift() function Shift index by desired number of periods with an optional time freq. This function takes a scalar parameter called the period, which represents the number of shifts to be made over the desired axis. This function is very helpful when dealing with time-series data.

How convert MultiIndex to columns in Pandas?

pandas MultiIndex to ColumnsUse pandas DataFrame. reset_index() function to convert/transfer MultiIndex (multi-level index) indexes to columns. The default setting for the parameter is drop=False which will keep the index values as columns and set the new index to DataFrame starting from zero.

What is shift in pandas Dataframe?

pandas.DataFrame.shift ¶ DataFrame.shift(periods=1, freq=None, axis=0, fill_value=<no_default>) [source] ¶ Shift index by desired number of periods with an optional time freq. When freq is not passed, shift the index without realigning the data.

How to access data in a multiindex Dataframe in pandas?

Accessing Data in a MultiIndex DataFrame in Pandas 1. Selecting data via the first level index When it comes to select data on a DataFrame, Pandas loc is one of the top... 2. Selecting data via multi-level index If you want to read London ’s Day weather on 2019–07–01, you can simply do: >>>... 3. ...

How do I shift the index of a Dataframe?

DataFrame.shift(periods=1, freq=None, axis=0, fill_value=<no_default>) [source] ¶ Shift index by desired number of periods with an optional time freq. When freq is not passed, shift the index without realigning the data.

How to shift the column axis to the negative direction in pandas?

Using shift () function in Pandas dataframe to shift the column axis to the negative direction. In the above program, we first import pandas as pd and afterward characterize the dataframe and rather than rowindex esteems, you will characterize all the columnindex esteems.


1 Answers

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.

like image 64
unutbu Avatar answered Oct 06 '22 02:10

unutbu