How can I transpose the pandas framework in a certain way?

For a data frame df:

     Col1  Col2
Key            
A       4    10
B       7    10
C       3     9

My desired data frame

            A   B  C                   
    Col1    4   7  3
    Col2   10  10  9

Where Col1and Col2are the indices.

How can I indicate this? I tried:

In [419]: mydf.T.reset_index(drop=True)
Out[419]: 
Key   A   B  C
0     4   7  3
1    10  10  9

But for some reason it remains Key. I'm not sure what it is, and I'm not sure how to get rid of it. I also tried mydf.T.reset_index().set_index('index'), but it is very unsightly.

+4
source share
1 answer

we can use DataFrame.rename_axis () here:

In [24]: df.T.rename_axis(None, axis=1)
Out[24]:
       A   B  C
Col1   4   7  3
Col2  10  10  9
+7
source

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


All Articles