Search code examples
pythonpandasmulti-indexreindex

How to reindex a multi-index pandas dataframe?


Given the following pandas.core.frame.DataFrame, called sorted_by_diff :

In [10]:sorted_by_diff.head(4)

Out[10]:
           value
y          0         1          diff
variable            
george     1.265265  0.001550   1.263716
hp         0.895473  0.017479   0.877994
hpl        0.431994  0.009173   0.422822
re         0.415760  0.125091   0.290669

Having the following columns:

In [11]: sorted_by_diff.columns
Out[11]:
MultiIndex(levels=[[u'value'], [0, 1, u'diff']],
           labels=[[0, 0, 0], [0, 1, 2]],
           names=[None, u'y'])

And the following indexes:

In [12]: sorted_by_diff.index

Out[12]:
Index([u'george', u'hp', u'hpl', u're', u'edu', u'meeting', u'650', u'85', u'lab', u'labs', u'1999', u'data', u'project', u'technology', u'pm', u'telnet', u'address', u'857', u'415', u'cs', u'original', u'(', u'conference', u'direct', u';', u'[', u'parts', u'table', u'will', u'report', u'#', u'make', u'people', u'receive', u'addresses', u'over', u'order', u'$', u'3d', u'internet', u'mail', u'font', u'money', u'credit', u'all', u'email', u'business', u'000', u'remove', u'our', u'!', u'free', u'your', u'you', u'length_average', u'length_longest', u'length_total'], dtype='object')

How to rexindex sorted_by_diff to be like this?

           value
y          email     spam       diff
variable            
george     1.265265  0.001550   1.263716
hp         0.895473  0.017479   0.877994
hpl        0.431994  0.009173   0.422822
re         0.415760  0.125091   0.290669

That is, how to change the index levels 0 and 1 to 'email' and 'spam', respectively?


Solution

  • >>> sorted_by_diff.columns.set_levels([[u'value'], ['email', 'spam', 'diff']], inplace=True)
    >>> sorted_by_diff
                 value                    
    y            email      spam      diff
    variable                              
    george    1.265265  0.001550  1.263716
    hp        0.895473  0.017479  0.877994
    hpl       0.431994  0.009173  0.422822
    re        0.415760  0.125091  0.290669