您好,我一直在尝试如何旋转我的行和列。我尝试使用转置,但它不起作用。我的数据帧如下所示
Country | Rates 2015 | Rates2016 | Rates2017 | GDP 2015| GDP 2016 | GDP2017
World | 6 | 7 | 8 | 2355 | 1235 | 324325可以更改为
| Rates | GDP
2015 | 6 | 2355
2016 | 7 | 1235
2017 | 8 | 34132是的,这就是我基本上想要做的。
发布于 2018-09-11 18:32:04
确保转置部分在索引中。我只能假设,如果您尝试过df.T,那么您没有正确地设置索引
In [185]: df.set_index('Country Name').T
Out[185]:
Country Name A B C
2000 5 2 2
2005 3 1 2
2010 1 5 6
2015 7 2 9如果你想给索引命名,你也需要这样做
In [187]: df = df.set_index('Country Name').T
In [188]: df.index = df.index.rename('Year')
In [189]: df
Out[189]:
Country Name A B C
Year
2000 5 2 2
2005 3 1 2
2010 1 5 6
2015 7 2 9https://stackoverflow.com/questions/52273801
复制相似问题