Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rotate pandas DataFrame 90 degrees

Tags:

python

pandas

Basically I want to rotate a pandas DataFrame by 90 degrees (clockwise), such that if it were

df:
    A B C D
  0 5 3 6 7
  1 6 3 5 2

it would turn into

df:
   6 5 A
   3 3 B
   5 6 C
   2 7 D

Is there some way to do this with pivots, or some other way? Thanks!

like image 549
TheoretiCAL Avatar asked May 29 '13 19:05

TheoretiCAL


1 Answers

transpose it

In [1]: df = DataFrame([[5,3,6,7],[6,3,5,2]],index=[0,1],columns=list('ABCD'))

In [2]: df
Out[2]: 
   A  B  C  D
0  5  3  6  7
1  6  3  5  2

In [3]: df.T
Out[3]: 
   0  1
A  5  6
B  3  3
C  6  5
D  7  2

I guess you REALLY want this

In [7]: df.T.reset_index().reindex(columns=[1,0,'index'])
Out[7]: 
   1  0 index
0  6  5     A
1  3  3     B
2  5  6     C
3  2  7     D
like image 76
Jeff Avatar answered Nov 01 '22 11:11

Jeff