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
Run Code Online (Sandbox Code Playgroud)
回报
a d
b c f
0 1 2 3
1 10 20 30
2 100 200 300
Run Code Online (Sandbox Code Playgroud)
和
df.columns.levels[1]
Run Code Online (Sandbox Code Playgroud)
回报
Index([u'b', u'c', u'f'], dtype='object')
Run Code Online (Sandbox Code Playgroud)
我想重命名"f"为"e".据pandas.MultiIndex.rename我说:
df.columns.rename(["b1", "c1", "f1"], level=1)
Run Code Online (Sandbox Code Playgroud)
但它提出了
---------------------------------------------------------------------------
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 …Run Code Online (Sandbox Code Playgroud) pandas ×1