I have a df with 48 columns and i want to rename them on two levels as in the dict below:
{'A': ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
'B': ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
'C': ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
'D': ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']}
i would like to get something like this: dataframe
CodePudding user response:
If you visit this doc, and read the see also section, you find 4 ways to build multiindex. Given your input which can be easily converted into a pd.DataFrame
, pd.MultiIndex.from_frame
should take the least coding effort.
df.columns = pd.MultiIndex.from_frame(pd.DataFrame(dictionary).melt())