0

I've a single data frame and after pivoting it I've got the dataframe in this format: enter image description here

I want it to be a single header like Raw01, Raw02, Raw 03, Raw04, Raw05, calcd01, calcd02, calcd03 and so on. Also the index column can just b a.

I've searched for other questions but in that case they have two data frames and I only have one. So, how will I achieve it?

1 Answer 1

1

You can flatten your columns like this:

df.columns = [''.join(map(str, col)).strip() for col in df.columns.values]
Sign up to request clarification or add additional context in comments.

4 Comments

I got a warning /anaconda3/lib/python3.6/site-packages/ipykernel_launcher.py:1: UserWarning: Pandas doesn't allow columns to be created via a new attribute name - see https://pandas.pydata.org/pandas-docs/stable/indexing.html#attribute-access """Entry point for launching an IPython kernel. and after that if I do df.head() it prints out the same multi index.
@ChaudhryTalha That's odd, df.columns should refer to columns not new column named columns. Did you paste the code correctly?
My bad... I'm with this dataset since morning and going crazy. It worked perfectly :D
@ChaudhryTalha No worries, glad to help :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.