简体   繁体   English

重塑Pandas DataFrame:将列切换为索引,并将重复值作为列

[英]Reshaping Pandas DataFrame: switch columns to indices and repeated values as columns

I've had a really tough time figuring out how to reshape this DataFrame. 我在弄清楚如何重塑此DataFrame方面非常艰难。 Sorry about the wording of the question, this problem seems a bit specific. 很抱歉问题的措辞,这个问题似乎有点具体。

I have data on several countries along with a column of 6 repeating features and the year this data was recorded. 我有几个国家/地区的数据,以及一列包含6个重复特征的数据,并且记录了该数据的年份。 It looks something like this (minus some features and columns): 看起来像这样(减去一些功能和列):

   Country        Feature           2005    2006    2007    2008    2009

0  Afghanistan    Age Dependency    99.0    99.5    100.0   100.2   100.1
1  Afghanistan    Birth Rate        44.9    43.9    42.8    41.6    40.3
2  Afghanistan    Death Rate        10.7    10.4    10.1    9.8     9.5
3  Albania        Age Dependency    53.5    52.2    50.9    49.7    48.7
4  Albania        Birth Rate        12.3    11.9    11.6    11.5    11.6
5  Albania        Death Rate        5.95    6.13    6.32    6.51    6.68

There doesn't seem to be any way to make pivot_table() work in this situation and I'm having trouble finding what other steps I can take to make it look how I want: 在这种情况下,似乎没有任何办法可以使pivot_table()正常工作,而且我很难找到可以采取其他步骤使其看起来像我想要的方式:

                       Age Dependency    Birth Rate    Death Rate

Afghanistan    2005    99.0              44.9          10.7
               2006    99.5              43.9          10.4
               2007    100.0             42.8          10.1   
               2008    100.2             41.6          9.8
               2009    100.1             40.3          9.5

Albania        2005    53.5              12.3          5.95
               2006    52.2              11.9          6.13
               2007    50.9              11.6          6.32
               2008    49.7              11.5          6.51
               2009    48.7              11.6          6.68

Where the unique values of the 'Feature' column each become a column and the year columns each become part of a multiIndex with the country. “功能”列的唯一值分别成为一列,而年份列分别成为与国家/地区的multiIndex的一部分。 Any help is appreciated, thank you! 任何帮助表示赞赏,谢谢!

EDIT: I checked the "duplicate" but I don't see how that question is the same as this one. 编辑:我检查了“重复”,但我看不到这个问题是如何与此相同。 How would I place the repeated values within my feature column as unique columns while at the same time moving the years to become a multi index with the countries? 我如何将重复值放在要素列中作为唯一列,同时又将年份变成与国家/地区的多元索引? Sorry if I'm just not getting something. 对不起,如果我没有得到任何东西。

Use melt with reshape by set_index and unstack : 通过set_index使用melt进行重塑并set_index unstack

df = (df.melt(['Country','Feature'], var_name='year')
      .set_index(['Country','year','Feature'])['value']
      .unstack())
print (df)
Feature           Age Dependency  Birth Rate  Death Rate
Country     year                                        
Afghanistan 2005            99.0        44.9       10.70
            2006            99.5        43.9       10.40
            2007           100.0        42.8       10.10
            2008           100.2        41.6        9.80
            2009           100.1        40.3        9.50
Albania     2005            53.5        12.3        5.95
            2006            52.2        11.9        6.13
            2007            50.9        11.6        6.32
            2008            49.7        11.5        6.51
            2009            48.7        11.6        6.68

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

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