How to remove row and rename multiindex table

I have multi index data frame like below and I would like remove the row above ‘A’ (like shift the dataframe up)

metric data data
        F    K
        C    B
 A      2    3
 B      4    5
 C      6    7
 D      8    9

desired output

ALIAS  data data
metric  F    K
 A      2    3
 B      4    5
 C      6    7
 D      8    9

I looked multiple post but could not find anything closer to create desired outcome. How can I achive the desired output ?

https://pandas.pydata.org/pandas-docs/stable/user_guide/advanced.html

Answer

Let’s try DataFrame.droplevel to remove level 2 from the columns, and DataFrame.rename_axis to update column axis names:

df = df.droplevel(level=2, axis=1).rename_axis(['ALIAS', 'metric'], axis=1)

Or with the index equivalent methods Index.droplevel and Index.rename:

df.columns = df.columns.droplevel(2).rename(['ALIAS', 'metric'])

df:

ALIAS  data   
metric    F  K
A         2  3
B         4  5
C         6  7
D         8  9

Setup:

import numpy as np
import pandas as pd

df = pd.DataFrame(
    np.arange(2, 10).reshape(-1, 2),
    index=list('ABCD'),
    columns=pd.MultiIndex.from_arrays([
        ['data', 'data'],
        ['F', 'K'],
        ['C', 'B']
    ], names=['metric', None, None])
)

df:

metric data   
          F  K
          C  B
A         2  3
B         4  5
C         6  7
D         8  9