Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reindex sublevel of pandas dataframe multiindex

I have a time series dataframe and I would like to reindex it by Trials and Measurements.

Simplified, I have this:

                value
Trial         
    1     0        13
          1         3
          2         4
    2     3       NaN
          4        12
    3     5        34   

Which I want to turn into this:

                  value
Trial    
    1      0        13
           1         3
           2         4
    2      0       NaN
           1        12
    3      0        34

How can I best do this?

like image 586
TheChymera Avatar asked Nov 20 '13 19:11

TheChymera


1 Answers

Just yesterday, the illustrious Andy Hayden added this feature to version 0.13 of pandas, which will be released any day now. See here for usage example he added to the docs.

If you are comfortable installing the development version of pandas from source, you can use it now.

df['Measurements'] = df.reset_index().groupby('Trial').cumcount()

The following code is equivalent, if less pithy, and will work on any recent version of pandas.

grouped = df.reset_index().groupby('Trial')
df['Measurements'] = grouped.apply(lambda x: Series(np.arange(len(x)), x.index))

Finally, df.set_index(['Trial', 'Measurements'], inplace=True) to get your desired result.

like image 96
Dan Allan Avatar answered Oct 04 '22 19:10

Dan Allan