I think you need unstack
+ reset_index
for reshape data:
#create `MultiIndex` DataFrame
df = pd.read_excel('HowItIs.xlsx', header=[0,1], index_col=[0])
#print (df)
#get names for new columns values
a, b = df.columns.names
d = {'level_2':'Category', 'Section Subheading':'Mesurement_Period_Week',
'SECTION TITLE':'Mesurement_Period'}
fin_order = ['Section_Title','Section_Subheading','Category','Mesurement_Period',
'Mesurement_Period_Week','Value']
df = (df.unstack()
.reset_index(name='Value')
.rename(columns=d)
.sort_values(['Category','Mesurement_Period','Mesurement_Period_Week'])
.assign(Section_Title = a, Section_Subheading=b)
.reset_index(drop=True)
[fin_order]
)
print (df.head())
Section_Title Section_Subheading Category Mesurement_Period \
0 SECTION TITLE Section Subheading Apples FY15 P03 Dec Count
1 SECTION TITLE Section Subheading Apples FY15 P03 Dec Count
2 SECTION TITLE Section Subheading Apples FY15 P03 Dec Count
3 SECTION TITLE Section Subheading Apples FY15 P03 Dec Count
4 SECTION TITLE Section Subheading Apples FY15 P03 Dec Weight
Mesurement_Period_Week Value
0 W1 53
1 W2 53
2 W3 53
3 W4 53
4 W1 2120
Last if need increment each value in Value
column per groups:
df['Value'] += df.groupby(['Category','Mesurement_Period']).cumcount()
print (df.head())
Section_Title Section_Subheading Category Mesurement_Period \
0 SECTION TITLE Section Subheading Apples FY15 P03 Dec Count
1 SECTION TITLE Section Subheading Apples FY15 P03 Dec Count
2 SECTION TITLE Section Subheading Apples FY15 P03 Dec Count
3 SECTION TITLE Section Subheading Apples FY15 P03 Dec Count
4 SECTION TITLE Section Subheading Apples FY15 P03 Dec Weight
Mesurement_Period_Week Value
0 W1 53
1 W2 54
2 W3 55
3 W4 56
4 W1 2120