在 Pandas 中交换轴

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

Swapping Axes in Pandas

pythonindexingpandasswap

提问by cmiller8

What is the most efficient way to swap the axes of a Pandas Dataframe?

交换 Pandas 数据框轴的最有效方法是什么?

For example, how could df1 be converted to df2 below?

例如,下面如何将 df1 转换为 df2?

In [2]: import pandas as pd

In [3]: df1 = pd.DataFrame({'one' : [1., 2., 3., 4.], 'two' : [4., 3., 2., 1.]})

In [4]: df1
Out[4]: 
   one  two
0    1    4
1    2    3
2    3    2
3    4    1

In [5]: df2 = pd.DataFrame({0 : [1,4], 1 : [2,3], 2 : [3,2], 3 : [4,1]}, index=['one','two'])

In [6]: df2
Out[6]: 
     0  1  2  3
one  1  2  3  4
two  4  3  2  1

回答by waitingkuo

Take a loot at transpose

转置中掠夺

In [4]: df1.T
Out[4]: 
     0  1  2  3
one  1  2  3  4 
two  4  3  2  1