I have the following Pandas dataframe taken from an Excel file (link to the Excel file)
I would like to flatten the Excel table with Pandas by converting the current headers (two first rows) to dataframe columns. This is where I want to get to:
segment unit category sub_category value
seg1 kg cat01 sub_cat_1.1 1
seg2 kg cat01 sub_cat_1.1 2
seg1 kg cat01 sub_cat_1.2 3
seg2 kg cat01 sub_cat_1.2
seg1 kg cat02 sub_cat_2.1 4
seg2 kg cat02 sub_cat_2.1 5
What I did so far is the folowing, but it doesn't work as expected:
import pandas as pd
_file_name = "stackoverflow_excel_data_example.xlsx"
df = pd.read_excel(_file_name, header=[0,1]).sort_index()
df = df.stack()
print(df)
Does anyone know how to convert a custom -kind of pivot- table to a flat dataframe?
