Rename MultiIndex columns to Pandas

df = pd.DataFrame([[1,2,3], [10,20,30], [100,200,300]]) df.columns = pd.MultiIndex.from_tuples((("a", "b"), ("a", "c"), ("d", "f"))) df 

returns

  ad bcf 0 1 2 3 1 10 20 30 2 100 200 300 

and

 df.columns.levels[1] 

returns

 Index([u'b', u'c', u'f'], dtype='object') 

I want to rename "f" to "e" . According to pandas.MultiIndex.rename I run:

 df.columns.rename(["b1", "c1", "f1"], level=1) 

But he raises

 --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-110-b171a2b5706c> in <module>() ----> 1 df.columns.rename(["b1", "c1", "f1"], level=1) C:\Users\USERNAME\AppData\Local\Continuum\Miniconda2\lib\site-packages\pandas\indexes\base.pyc in set_names(self, names, level, inplace) 994 if level is not None and not is_list_like(level) and is_list_like( 995 names): --> 996 raise TypeError("Names must be a string") 997 998 if not is_list_like(names) and level is None and self.nlevels > 1: TypeError: Names must be a string 

I am using Python 2.7.12 |Continuum Analytics, Inc.| (default, Jun 29 2016, 11:07:13) [MSC v.1500 64 bit (AMD64)]' Python 2.7.12 |Continuum Analytics, Inc.| (default, Jun 29 2016, 11:07:13) [MSC v.1500 64 bit (AMD64)]' and pandas 0.19.1

+6
source share
2 answers

Use set_levels :

 In [22]: df.columns.set_levels(['b1','c1','f1'],level=1,inplace=True) df Out[22]: ad b1 c1 f1 0 1 2 3 1 10 20 30 2 100 200 300 

rename sets the name for the index, it does not rename the column names:

 In [26]: df.columns = df.columns.rename("b1", level=1) df Out[26]: ad b1 bcf 0 1 2 3 1 10 20 30 2 100 200 300 

This is why you get an error message

+6
source

In pandas 0.21.0+ use the parameter level=1 :

 d = dict(zip(df.columns.levels[1], ["b1", "c1", "f1"])) print (d) {'c': 'c1', 'b': 'b1', 'f': 'f1'} df = df.rename(columns=d, level=1) print (df) ad b1 c1 f1 0 1 2 3 1 10 20 30 2 100 200 300 
+2
source

Source: https://habr.com/ru/post/1013283/


All Articles