Search code examples
pandasmulti-index

Pandas Multilevel index for rows


This should be a simple thing, but after a few hours of searching, I'm still at a loss for what I'm doing wrong.

I've tried different methods using MultiIndexing.from_ and multiple other things, but I just can't get this right.

I need something like:
enter image description here

But instead I get:
enter image description here What am I doing wrong?

import pandas as pd

list_of_customers = ['Client1', 'Client2', 'Client3']
stat_index = ['max', 'current', 'min']
list_of_historic_timeframes = ['16:10', '16:20', '16:30']

timeblock = pd.DataFrame(index=([list_of_customers, stat_index]), columns=list_of_historic_timeframes)
timeblock.fillna(0, inplace=True)

print(timeblock)

Solution

  • list_of_customers = ['Client1', 'Client2', 'Client3']
    stat_index = ['max', 'current', 'min']
    list_of_historic_timeframes = ['16:10', '16:20', '16:30']
    
    
    timeblock = pd.DataFrame(
        0,
        pd.MultiIndex.from_product(
            [list_of_customers, stat_index],
            names=['Customer', 'Stat']
        ),
        list_of_historic_timeframes
    )
    
    print(timeblock)
    
                      16:10  16:20  16:30
    Customer Stat                        
    Client1  max          0      0      0
             current      0      0      0
             min          0      0      0
    Client2  max          0      0      0
             current      0      0      0
             min          0      0      0
    Client3  max          0      0      0
             current      0      0      0
             min          0      0      0