I have a large dataframe with lots of columns, each with their own header. I'd like to group them in sub headers so readability is more clear. For example, here are my column headers:
df1 = df1[['Date', 'Time', 'USA', 'Canada', 'SD', 'HD']]
I'd like have headers above certain columns to output like this:
When Country Channel
Date Time USA Canada SD HD
However, I'm not sure how to go about this. Any assistance or direction is much appreciated.
Thanks!
CodePudding user response:
You can use:
df.columns = pd.MultiIndex.from_tuples([('When', 'Date'), ('When', 'Time'),
('Country', 'USA'), ('Country', 'Canada'),
('Channel', 'SD'), ('Channel', 'HD')])