Search code examples
pythonpandaspivot-tablemulti-index

Grouping levels - multiindex in python pandas pivot_table


I've a multiindex dataframe in pandas that looks this (created using pivot_table):

enter image description here

I need help to add a level above (or below) the Date level showing the day of the date like this:

enter image description here

I know I can get the day of a date like this:

lt.DATE.dt.strftime('%a')
#lt is a dataframe and DATE is a column it.

Here is the code reporduce a similar pivot_table:

import pandas as pd
import numpy as np

dlist = pd.date_range('2015-01-01',periods=5)
df = pd.DataFrame(dlist, columns=['DATE'])
df['EC'] = range(7033,7033+len(df))
df['HS'] = np.random.randint(0,9,5)
df['AH'] = np.random.randint(0,9,5)
pv = pd.pivot_table(df, columns=[df.DATE, 'EC'], values=['HS','AH'])
pv = pv.unstack(level=1).unstack(level=0)

Solution

  • I got the solution! Here it goes:

    import pandas as pd
    import numpy as np
    
    dlist = pd.date_range('2015-01-01',periods=5)
    df = pd.DataFrame(dlist, columns=['DATE'])
    df['EC'] = range(7033,7033+len(df))
    df['HS'] = np.random.randint(0,9,5)
    df['AH'] = np.random.randint(0,9,5)
    df['DAY'] = df.DATE.dt.strftime('%a')
    pv = pd.pivot_table(df, columns=[df.DATE.dt.date, df.DAY, 'EC'], values=['HS','AH'])
    pv = pv.unstack(level=[1,2]).unstack(level=0)
    pv.to_excel('solution.xlsx')
    

    And it produces an output like this:

    enter image description here

    Pay attention to the function unstack and set the list of levels that are required to be unstacked at a time.