将 Pandas DataFrame 旋转 90 度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16822066/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 20:51:47 来源:igfitidea点击:
Rotate pandas DataFrame 90 degrees
提问by TheoretiCAL
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!
有没有办法用枢轴或其他方式来做到这一点?谢谢!
回答by Jeff
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

