Bhaskar Dhariyal
Bhaskar Dhariyal

Reputation: 1395

How should I merge an index with header name in pandas?

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

Upvotes: 3

Views: 170

Answers (1)

anky
anky

Reputation: 75080

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

Upvotes: 2

Related Questions