简体   繁体   English

我该如何旋转这个Pandas DataFrame?

[英]How can I pivot this Pandas DataFrame?

I am reading a csv file which have the following structure: 我正在读取具有以下结构的csv文件:

Continent, Country, Year, GDP

All countries have multiple years but some countries might missing some years. 所有国家都有多年,但有些国家可能会错过一些年。

My aim is to have as index the Continent and Country, and as columns the GDP for each year. 我的目标是将大陆和国家作为索引,并将每年的GDP作为列。

Continent Country 2009 2010 2011 2012 2013 2014

I have tried this: 我已经试过了:

df.pivot(index=["Continent", "Country"], columns="Year", values="GDP")

but it gives me this error: 但这给了我这个错误:

ValueError: Buffer has wrong number of dimensions (expected 1, got 2)

You can try this sample data: 您可以尝试以下示例数据:

pd.DataFrame(columns=['Continent', 'Country', 'Year', 'GDP'],
             data=[['NA', 'US', 2014, 1234], ['NA', 'US', 2013, 2345]])

If you use pivot_table instead of pivot , it works: 如果使用pivot_table而不是pivot ,它将起作用:

In [47]: df.pivot_table(index=["Continent", "Country"], columns="Year", values="GDP")
Out[47]:
Year               2013  2014
Continent Country
NA        US       2345  1234

The problem is that pivot cannot handle a list of columns for the index/columns argument. 问题是pivot无法处理index / columns参数的列列表。 The only caveat is that now the default is to take the mean if there are multiple values for one continent/country/year combination. 唯一的警告是,如果一个大洲/国家/地区/年份组合有多个值,则默认值为均值。

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

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