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!
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With