6

Having the following dataframe,

      0      1      2
A    0.2    0.4    0.6
B    0.1    0.1    0.3

How to achieve this transformation, while merging the row index with column name?

     A_0    A_1    A_2    B_0    B_1    B_2
0    0.2    0.4    0.6    0.1    0.1    0.3

1 Answer 1

12

Use stack followed by a transpose to get the DataFrame in the right shape, then format the column names as appropriate.

df = df.stack().to_frame().T
df.columns = ['{}_{}'.format(*c) for c in df.columns]
Sign up to request clarification or add additional context in comments.

Comments

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.