Search code examples
pythonpandasmulti-index

Multilevel index won't go away


I have a dataframe, which consists of summary statistics of another dataframe:

df = sample[['Place','Lifeexp']]
df = df.groupby('Place').agg(['count','mean', 'max','min']).reset_index()
df = df.sort_values([('Lifeexp', 'count')], ascending=False)

When looking at the structure, the dataframe has a multi index, which makes plot creations difficult:

df.columns

MultiIndex(levels=[['Lifeexp', 'Place'], ['count', 'mean', 'max', 'min', '']],
           labels=[[1, 0, 0, 0, 0], [4, 0, 1, 2, 3]])

I tried the solutions of different questions here (e.g. this), but somehow don't get the desired result. I want df to have Place, count, mean,max, min as column names and delete Lifeexp so that I can create easy plots e.g. df.plot.bar(x = "Place", y = 'count')


Solution

  • I think solution should be simplify define column after groupby for prevent MultiIndex in columns:

    df = df.groupby('Place')['Lifeexp'].agg(['count','mean', 'max','min']).reset_index()
    
    df = df.sort_values('count', ascending=False)