Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a DataFrame with a MultiIndex

I would like to make my DataFrame like the one below and export it to excel. I have all the data available for all the '-' that I have put. I want to know what data structure to pass to pd.Dataframe() to make a table like this.

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

enter image description here

like image 699
stormtrooper12 Avatar asked Aug 11 '16 15:08

stormtrooper12


People also ask

Can a DataFrame have 2 indexes?

You can also construct a MultiIndex from a DataFrame directly, using the method MultiIndex. from_frame() .


1 Answers

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 screen shot

enter image description here

df.to_excel('test.xlsx')

Mac Numbers screen shot

enter image description here

like image 139
piRSquared Avatar answered Sep 24 '22 14:09

piRSquared