Swapping Axes in Pandas

Question:

What is the most efficient way to swap the axes of a Pandas Dataframe?

For example, how could df1 be converted to df2 below?

In [2]: import pandas as pd

In [3]: df1 = pd.DataFrame({'one' : [1., 2., 3., 4.], 'two' : [4., 3., 2., 1.]})

In [4]: df1
Out[4]: 
   one  two
0    1    4
1    2    3
2    3    2
3    4    1

In [5]: df2 = pd.DataFrame({0 : [1,4], 1 : [2,3], 2 : [3,2], 3 : [4,1]}, index=['one','two'])

In [6]: df2
Out[6]: 
     0  1  2  3
one  1  2  3  4
two  4  3  2  1
Asked By: Clayton

||

Answers:

Take a look at transpose

In [4]: df1.T
Out[4]: 
     0  1  2  3
one  1  2  3  4 
two  4  3  2  1
Answered By: waitingkuo
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.