<h4 dir="auto">Code Sample, a copy-pastable example if possible</h4>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="mi = pd.MultiIndex.from_product([['a','b','c'],[1,2]], names=['ll','nn'])
df = pd.DataFrame({'x': [i for i in range(len(mi))], 'y' : [i*10 for i in range(len(mi))]}, index=mi)
df.rename(columns={'x':'z'})  # This works to change name of one column
df.rename_axis({'x' : 'z'}, axis='columns')  # This also works to change name of one column (but you get a deprecation warning)
df.rename(index={'nn':'zz'}) # This does not work to change name of one level of a MultiIndex
df.rename_axis({'nn' : 'zz'}, axis='index') # This does not work to change name of one level of a MultiIndex"><pre class="notranslate"><span class="pl-s1">mi</span> <span class="pl-c1">=</span> <span class="pl-s1">pd</span>.<span class="pl-v">MultiIndex</span>.<span class="pl-en">from_product</span>([[<span class="pl-s">'a'</span>,<span class="pl-s">'b'</span>,<span class="pl-s">'c'</span>],[<span class="pl-c1">1</span>,<span class="pl-c1">2</span>]], <span class="pl-s1">names</span><span class="pl-c1">=</span>[<span class="pl-s">'ll'</span>,<span class="pl-s">'nn'</span>])
<span class="pl-s1">df</span> <span class="pl-c1">=</span> <span class="pl-s1">pd</span>.<span class="pl-v">DataFrame</span>({<span class="pl-s">'x'</span>: [<span class="pl-s1">i</span> <span class="pl-k">for</span> <span class="pl-s1">i</span> <span class="pl-c1">in</span> <span class="pl-en">range</span>(<span class="pl-en">len</span>(<span class="pl-s1">mi</span>))], <span class="pl-s">'y'</span> : [<span class="pl-s1">i</span><span class="pl-c1">*</span><span class="pl-c1">10</span> <span class="pl-k">for</span> <span class="pl-s1">i</span> <span class="pl-c1">in</span> <span class="pl-en">range</span>(<span class="pl-en">len</span>(<span class="pl-s1">mi</span>))]}, <span class="pl-s1">index</span><span class="pl-c1">=</span><span class="pl-s1">mi</span>)
<span class="pl-s1">df</span>.<span class="pl-en">rename</span>(<span class="pl-s1">columns</span><span class="pl-c1">=</span>{<span class="pl-s">'x'</span>:<span class="pl-s">'z'</span>})  <span class="pl-c"># This works to change name of one column</span>
<span class="pl-s1">df</span>.<span class="pl-en">rename_axis</span>({<span class="pl-s">'x'</span> : <span class="pl-s">'z'</span>}, <span class="pl-s1">axis</span><span class="pl-c1">=</span><span class="pl-s">'columns'</span>)  <span class="pl-c"># This also works to change name of one column (but you get a deprecation warning)</span>
<span class="pl-s1">df</span>.<span class="pl-en">rename</span>(<span class="pl-s1">index</span><span class="pl-c1">=</span>{<span class="pl-s">'nn'</span>:<span class="pl-s">'zz'</span>}) <span class="pl-c"># This does not work to change name of one level of a MultiIndex</span>
<span class="pl-s1">df</span>.<span class="pl-en">rename_axis</span>({<span class="pl-s">'nn'</span> : <span class="pl-s">'zz'</span>}, <span class="pl-s1">axis</span><span class="pl-c1">=</span><span class="pl-s">'index'</span>) <span class="pl-c"># This does not work to change name of one level of a MultiIndex</span></pre></div>
<h4 dir="auto">Problem description</h4>
<p dir="auto">Ideally, if you call <code class="notranslate">df.rename_axis(dict, axis='index')</code>, only the names specified in the dictionary would be changed in the corresponding <code class="notranslate">MultiIndex</code></p>
<h4 dir="auto">Expected Output</h4>
<p dir="auto">For the expression</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="df.rename_axis({'nn' : 'zz'}, axis='index')"><pre class="notranslate"><code class="notranslate">df.rename_axis({'nn' : 'zz'}, axis='index')
</code></pre></div>
<p dir="auto">the output should be:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="       x   y
zz ll       
1  a   0   0
   b   1  10
   c   2  20
2  a   3  30
   b   4  40
   c   5  50"><pre class="notranslate"><code class="notranslate">       x   y
zz ll       
1  a   0   0
   b   1  10
   c   2  20
2  a   3  30
   b   4  40
   c   5  50
</code></pre></div>
<h4 dir="auto">Output of <code class="notranslate">pd.show_versions()</code></h4>
<details>
<h2 dir="auto">INSTALLED VERSIONS</h2>
<p dir="auto">commit: None<br>
python: 3.6.4.final.0<br>
python-bits: 64<br>
OS: Windows<br>
OS-release: 10<br>
machine: AMD64<br>
processor: Intel64 Family 6 Model 60 Stepping 3, GenuineIntel<br>
byteorder: little<br>
LC_ALL: None<br>
LANG: None<br>
LOCALE: None.None</p>
<p dir="auto">pandas: 0.22.0<br>
pytest: 3.3.2<br>
pip: 9.0.1<br>
setuptools: 38.4.0<br>
Cython: 0.27.3<br>
numpy: 1.14.0<br>
scipy: 1.0.0<br>
pyarrow: None<br>
xarray: None<br>
IPython: 6.2.1<br>
sphinx: 1.6.6<br>
patsy: 0.5.0<br>
dateutil: 2.6.1<br>
pytz: 2017.3<br>
blosc: None<br>
bottleneck: 1.2.1<br>
tables: 3.4.2<br>
numexpr: 2.6.4<br>
feather: None<br>
matplotlib: 2.1.2<br>
openpyxl: 2.4.10<br>
xlrd: 1.1.0<br>
xlwt: 1.3.0<br>
xlsxwriter: 1.0.2<br>
lxml: 4.1.1<br>
bs4: 4.6.0<br>
html5lib: 1.0.1<br>
sqlalchemy: 1.2.1<br>
pymysql: 0.7.11.None<br>
psycopg2: None<br>
jinja2: 2.10<br>
s3fs: None<br>
fastparquet: None<br>
pandas_gbq: None<br>
pandas_datareader: None<br>
In [ ]:</p>
<p dir="auto">​</p>
</details>
<p dir="auto">xref <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="174840894" data-permission-text="Title is private" data-url="https://github.com/pandas-dev/pandas/issues/14139" data-hovercard-type="issue" data-hovercard-url="/pandas-dev/pandas/issues/14139/hovercard" href="https://github.com/pandas-dev/pandas/issues/14139">#14139</a> for empty MI</p>
<p dir="auto">Hi everybody,</p>
<p dir="auto">in the current version renaming of MultiIndex DataFrames does not work. Lets take the following example:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="import datetime as DT
import pandas as pd
df = pd.DataFrame({
'Branch' : 'A A A A A B'.split(),
'Buyer': 'Carl Mark Carl Joe Mark Carl'.split(),
'Quantity': [1,3,5,8,9,3],
'Date' : [
    DT.datetime(2013,9,1,13,0),
    DT.datetime(2013,9,1,13,5),
    DT.datetime(2013,10,1,20,0),
    DT.datetime(2013,10,3,10,0),
    DT.datetime(2013,12,2,12,0),                                      
    DT.datetime(2013,12,2,14,0),
    ]})"><pre class="notranslate"><code class="notranslate">import datetime as DT
import pandas as pd
df = pd.DataFrame({
'Branch' : 'A A A A A B'.split(),
'Buyer': 'Carl Mark Carl Joe Mark Carl'.split(),
'Quantity': [1,3,5,8,9,3],
'Date' : [
    DT.datetime(2013,9,1,13,0),
    DT.datetime(2013,9,1,13,5),
    DT.datetime(2013,10,1,20,0),
    DT.datetime(2013,10,3,10,0),
    DT.datetime(2013,12,2,12,0),                                      
    DT.datetime(2013,12,2,14,0),
    ]})
</code></pre></div>
<p dir="auto">and the following query:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="test_df = df[df['Buyer'].isin(['Carl', 'Mark'])].set_index('Buyer', append=True)[['Date']].unstack(['Buyer'])"><pre class="notranslate"><code class="notranslate">test_df = df[df['Buyer'].isin(['Carl', 'Mark'])].set_index('Buyer', append=True)[['Date']].unstack(['Buyer'])
</code></pre></div>
<p dir="auto">Now, the following renaming does not work</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="test_df.rename(columns={('Date', 'Carl'): 'Carl'}, inplace=True)"><pre class="notranslate"><code class="notranslate">test_df.rename(columns={('Date', 'Carl'): 'Carl'}, inplace=True)
</code></pre></div>
<p dir="auto">Thanks in advance</p>
<p dir="auto">Andy</p>