0

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!

1 Answer 1

4

You can use:

df.columns = pd.MultiIndex.from_tuples([('When', 'Date'), ('When', 'Time'),
                                        ('Country', 'USA'), ('Country', 'Canada'),
                                        ('Channel', 'SD'), ('Channel', 'HD')])
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! Exactly what I was looking for!
Plz consider accepting the answer if it helped.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.