Convert row names to column in Pandas

I have the following Pandas data frame :

print(df) head1 head2 head3 bar 32 3 100 bix 22 NaN NaN foo 11 1 NaN qux NaN 10 NaN xoo NaN 2 20 

What I want to do is convert the bar, bix, ... row names to such columns that in the end I have something like this:

  newhead head1 head2 head3 0 bar 32 3 100 1 bix 22 NaN NaN 2 foo 11 1 NaN 3 qux NaN 10 NaN 4 xoo NaN 2 20 

How can i achieve this?

+5
source share
1 answer
 df.index.name = 'newhead' df.reset_index(inplace=True) 

gives

  newhead head1 head2 head3 0 bar 32 3 100 1 bix 22 NaN NaN 2 foo 11 1 NaN 3 qux NaN 10 NaN 4 xoo NaN 2 20 
+17
source

Source: https://habr.com/ru/post/1200966/


All Articles