反转 Pandas DataFrame 中的索引和列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17076705/
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:54:43  来源:igfitidea点击:

Invert index and columns in a pandas DataFrame

pythonpandas

提问by nom-mon-ir

I have a pandas DataFrame with a single row:

我有一个单行的Pandas数据帧:

         10  20  30  70
data1:  2.3   5   6   7

I want to reindex the frame so that the column values (10, 20, 30, 70) become index values and the data becomes the column:

我想重新索引框架,以便列值(10、20、30、70)成为索引值,数据成为列:

    data1:
10     2.3
20     5.0
30     6.0
70     7.0

How do I achieve this?

我如何实现这一目标?

回答by Andy Hayden

You're looking for the transpose(T) method:

您正在寻找transpose( T) 方法:

In [11]: df
Out[11]:
         10  20  30  70
data1:  2.3   5   6   7

In [12]: df.T
Out[12]:
    data1:
10     2.3
20     5.0
30     6.0
70     7.0