Creating a DataFrame with MultiIndex

I would like to make my DataFrame look like the one below and export it to excel. I have all the data available to all the "-" that I put. I want to know which data structure to pass to pd.Dataframe () in order to create a table like this.

I would like to know how pandas read these data structures to form a DataFrame.

enter image description here

+4
source share
1 answer
idx = pd.MultiIndex.from_product([['Zara', 'LV', 'Roots'],
                                  ['Orders', 'GMV', 'AOV']],
                                 names=['Brand', 'Metric'])
col = ['Yesterday', 'Yesterday-1', 'Yesterday-7', 'Thirty day average']

df = pd.DataFrame('-', idx, col)
df

Jupyter Screenshot

enter image description here

df.to_excel('test.xlsx')

Mac Numbers Screen

enter image description here

+12
source

Source: https://habr.com/ru/post/1650937/


All Articles