Search code examples
pandaspivot-tablereshape

How to reshape a Pivot Table?


I am trying to "reshape" a pivot table:

enter image description here

I would like to remove those MultiIndex:

enter image description here

This should be my output:

I have already tried stack, unstack, melt, but I am not getting the result.
At the moment I am saving the Pivot in a CSV, opening the Excel file and deleting manually those extra cells and lines to re-upload on my jupyter (I know it's not a descent job)

Can anybody help please?


Solution

  • Try:

    brincando3 = df4.pivot_table('OCCUPANCY %', 'NAME', 'NEW_TIME', aggfunc='median') \
                    .rename_axis(columns=None)
    
    1. Don't use ['OCCUPANCY %'] if you don't want an outer level, prefer 'OCCUPANCY %' if you have only one variable to pivot.

    2. NEW_TIME is the column label. To remove it, use rename_axis(columns=None).