Search code examples
pythonpandasmulti-index

Changing second level multiindex label when first level condition is met


I have a multi index df with two levels of index like this:

    dfx = pd.DataFrame(np.random.rand(4, 2),
                  index=[['a', 'a', 'b', 'b'], ['aa', 'zz', 'gg', 'zz']],
                  columns=['data1', 'data2'])
    dfx

         data1     data2
a aa  0.847741  0.723235
  zz  0.236876  0.343141
b gg  0.759153  0.546190
  zz  0.481285  0.600514

I want to change the index labels only where the first level index has a specific value. i.e. Change the zz index only where the first label is b

Objective, get:

         data1     data2
a aa    0.847741  0.723235
  zz    0.236876  0.343141
b gg    0.759153  0.546190
  water 0.481285  0.600514

If I use .rename() all of the indexes matching get changed

dfx.rename(index={('zz') : 'water'}, inplace = True)
dfx

            data1     data2
a aa     0.847741  0.723235
  water  0.236876  0.343141
b gg     0.759153  0.546190
  water  0.481285  0.600514

I've tried the following code lines but this doesn't seem to do anything.

dfx.loc['b','zz'].rename(index={'zz' : 'water'}, inplace = True)
dfx.loc['b'].rename(index={'zz' : 'water'},  inplace = True)

I consulted the documentation and I've struggled to find a solution. What am I doing wrong here?


Solution

  • We can use MultiIndex.map

    d = {('b', 'zz'): ('b', 'water')}
    dfx.index = dfx.index.map(lambda i: d.get(i, i))
    

                data1     data2
    a aa     0.567847  0.844618
      zz     0.752874  0.794704
    b gg     0.854358  0.512400
      water  0.237905  0.211369