Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting a pandas MultiIndex DataFrame from rows-wise to column-wise

I'm working in zipline and pandas and have converted a pandas.Panel to a pandas.DataFrame using the to_frame() method. This is the resulting pandas.DataFrame which as you can see is multi-indexed:

                                  price
major                     minor                
2008-01-03 00:00:00+00:00 SPY    129.93
                          KO      26.38
                          PEP     64.78
2008-01-04 00:00:00+00:00 SPY    126.74
                          KO      26.43
                          PEP     64.59
2008-01-07 00:00:00+00:00 SPY    126.63
                          KO      27.05
                          PEP     66.10
2008-01-08 00:00:00+00:00 SPY    124.59
                          KO      27.16
                          PEP     66.63

I need to convert this frame to look like this:

                          SPY     KO     PEP
2008-01-03 00:00:00+00:00 129.93  26.38  64.78
2008-01-04 00:00:00+00:00 126.74  26.43  64.59
2008-01-07 00:00:00+00:00 126.63  27.05  66.10
2008-01-08 00:00:00+00:00 124.59  27.16  66.63

I've tried the pivot method, stack/unstack, etc. but these methods are not what I'm looking for. I'm really quite stuck at this point and any help is appreciated.

like image 958
Jason Strimpel Avatar asked Apr 01 '13 20:04

Jason Strimpel


People also ask

How do I convert MultiIndex to columns?

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.

How do I convert rows to columns in pandas?

The transpose() function is used to transpose index and columns. Reflect the DataFrame over its main diagonal by writing rows as columns and vice-versa. If True, the underlying data is copied.

How do I turn a DataFrame index into a column?

In order to set index to column in pandas DataFrame use reset_index() method. By using this you can also set single, multiple indexes to a column. If you are not aware by default, pandas adds an index to each row of the pandas DataFrame.

How do I convert MultiIndex to single index in pandas?

To revert the index of the dataframe from multi-index to a single index using the Pandas inbuilt function reset_index(). Returns: (Data Frame or None) DataFrame with the new index or None if inplace=True.


1 Answers

Because you have a MultiIndex in place already, stack and unstack are what you want to use to move rows to cols and vice versa. That being said, unstack should do exactly what you want to accomplish. If you have a DataFrame df then df2 = df.unstack('minor') should do the trick. Or more simply, since by default stack/unstack use the innermost level, df2 = df.unstack().

like image 200
bdiamante Avatar answered Sep 23 '22 16:09

bdiamante