3

I'm trying to merge the index with header name. My data frame is like

t  = pd.DataFrame({'A': ['a', 'b']})
t

    A
0   a
1   b

But the desired output is

    A_0     A_1
0   a       b

1 Answer 1

2

Try with unstack() , then transpose , then join the multiindex columns:

final=t.unstack().to_frame().T
final.columns=['_'.join(map(str,i)) for i in final.columns]
print(final)

  A_0 A_1
0   a   b
Sign up to request clarification or add additional context in comments.

2 Comments

I was about to post the same verbatim answer with even variable names :)
@iDrwish aha..co-incidence ;)

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.