1

I wanted to respond and extend this post in order to stick together all the similar information about transpose. It was said to me that I have to make a separated question.

Swapping Axes in Pandas

If you have a simple DF you have two default axis: 'index' and 'columns'. Then you can use methods or functions like this:

df1 = pd.DataFrame({'one' : [1., 2., 3., 4.], 'two' : [4., 3., 2., 1.]})
df1.T
df1.transpose()
df1.swapaxes(axis1= 'columns',axis2= 'index')
# Edit: adding examples 
df1
Out[98]: 
   one  two
0  1.0  4.0
1  2.0  3.0
2  3.0  2.0
3  4.0  1.0

df1.T
Out[99]: 
       0    1    2    3
one  1.0  2.0  3.0  4.0
two  4.0  3.0  2.0  1.0

df1.one
Out[100]: 
0    1.0
1    2.0
2    3.0
3    4.0
Name: one, dtype: float64

df1.one.equals(df1.one.T)
Out[106]: True

But since Series have an 'index' axis but not a 'columns' axis how could be possible to transpose it?.

I hope this time I'm following all the SO recomendation about asking.

Jony
  • 51
  • 9

1 Answers1

0

Here's one way:

s = df1['one'].to_frame().T

OUTPUT:

       0    1    2    3
one  1.0  2.0  3.0  4.0
Nk03
  • 14,699
  • 2
  • 8
  • 22