Search code examples
pythonpandasrenamedataframemulti-index

python pandas: rename single column label in multi-index dataframe


I have a df that looks like this:

df = pd.DataFrame(np.random.random((4,4)))
df.columns = pd.MultiIndex.from_product([['1','2'],['A','B']])
print df
          1                   2          
          A         B         A         B
0  0.030626  0.494912  0.364742  0.320088
1  0.178368  0.857469  0.628677  0.705226
2  0.886296  0.833130  0.495135  0.246427
3  0.391352  0.128498  0.162211  0.011254

How can I rename column '1' and '2' as 'One' and 'Two'?

I thought df.rename() would've helped but it doesn't. Have no idea how to do this?


Solution

  • That is indeed something missing in rename (ideally it should let you specify the level).
    Another way is by setting the levels of the columns index, but then you need to know all values for that level:

    In [41]: df.columns.levels[0]
    Out[41]: Index([u'1', u'2'], dtype='object')
    
    In [43]: df.columns = df.columns.set_levels(['one', 'two'], level=0)
    
    In [44]: df
    Out[44]:
            one                 two
              A         B         A         B
    0  0.899686  0.466577  0.867268  0.064329
    1  0.162480  0.455039  0.736870  0.759595
    2  0.620960  0.922119  0.060141  0.669997
    3  0.871107  0.043799  0.080080  0.577421
    
    In [45]: df.columns.levels[0]
    Out[45]: Index([u'one', u'two'], dtype='object')