简体   繁体   English

将pandas DataFrame旋转90度

[英]Rotate pandas DataFrame 90 degrees

Basically I want to rotate a pandas DataFrame by 90 degrees (clockwise), such that if it were 基本上我想将pandas DataFrame旋转90度(顺时针),如果是的话

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM