【发布时间】:2017-03-05 22:45:41
【问题描述】:
当前数据框:
CountryName IndicatorCode Year Value
Arab World TX.VAL.MRCH.RS.ZS 1960 1.646954e+01
Arab World TX.VAL.MRCH.R1.ZS 1960 2.260207e+00
Arab World TX.VAL.MRCH.RS.ZS 1961 1.244584e+01
Arab World TX.VAL.MRCH.R1.ZS 1961 1.860104e+00
Zimbabwe DT.DIS.OFFT.CD 2015 8.377700e+07
Zimbabwe DT.INT.OFFT.CD 2015 2.321300e+07
Zimbabwe DT.AMT.PROP.CD 2015 6.250000e+05
我想将 IndicatorCode 列的每个值转换为不同的列,这些列应包含来自 Value 列的相应行的数据。
比如做reshape之后:
CountryName Year TX.VAL.MRCH.RS.ZS TX.VAL.MRCH.R1.ZS
Arab World 1960 1.646954e+01 2.260207e+00
Arab World 1961 1.244584e+01 1.860104e+00
最终数据框列应为:
[CountryName, Year, TX.VAL.MRCH.RS.ZS, TX.VAL.MRCH.R1.ZS, DT.DIS.OFFT.CD,DT.INT.OFFT.CD, DT.AMT.PROP.CD]
我尝试使用 pivot,但没有成功。我也不能将国家名称作为索引,因为它不是唯一的。
temp = indicators_df.pivot(columns='IndicatorCode', values='Value')
得到ValueError: negative dimensions are not allowed
【问题讨论】: