Replacing values in a pandas multi-index
<p>I have a dataframe with a multi-index. I want to change the value of the 2nd index when certain conditions on the first index are met.
I found a similar (but different) question here: <a href="https://stackoverflow.com/questions/31072305/replace-a-value-in-multiindex-pandas">Replace a value in MultiIndex (pandas)</a>
which doesn't answer my point because that was about changing a single row, and the solution passed the value of the first index (which didn't need changing), too. In my case I am dealing with multiple rows and I haven't been able to adapt that solution to my case.</p>

<p>A minimal example of my data is below. Thanks!</p>

<pre><code>import pandas as pd
import numpy as np

consdf=pd.DataFrame()

for mylocation in ['North','South']:
    for scenario in np.arange(1,4):
        df= pd.DataFrame()
        df['mylocation'] = [mylocation]
        df['scenario']= [scenario]
        df['this'] = np.random.randint(10,100)
        df['that'] = df['this']  * 2
        df['something else']  = df['this'] * 3
        consdf=pd.concat((consdf, df ), axis=0, ignore_index=True)

mypiv = consdf.pivot('mylocation','scenario').transpose()

level_list =['this','that']
# if level 0 is in level_list --&gt; set level 1 to np.nan
mypiv.iloc[mypiv.index.get_level_values(0).isin(level_list)].index.set_levels([np.nan], level =1, inplace=True)
</code></pre>

<p>The last line doesn't work: I get:</p>

<pre><code>ValueError: On level 1, label max (2) &gt;= length of level  (1). NOTE: this index is in an inconsistent state
</code></pre>