问题描述
我正在尝试重现解决方案 from this answer,但在某处犯了错误。
'Region','Region code','Subregion','Country','Country code','IDA Status','FCV Status','Landlocked Status','Pillar','Indicator','Year','Value','Status','Measurement','Updated','Sources'
我需要将两个长列转换为宽格式:
Indicator Status Value
Indicator 1 Actual 20
Indicator 2 Actual 30
Indicator 3 Actual 40
Indicator 1 Forecast 30
Indicator 2 Forecast 40
Indicator 3 Forecast 50
Indicator 1 Target 60
Indicator 2 Target 60
Indicator 3 Target 60
我想把它从 2 列变成 9 列
Indicator1_Actual | Indicator1_Forecast | Indicator1_Target | Indicator2_Actual
20 30 60 60
等
我正在根据上面的问题解决问题
foo['idx'] = foo.groupby('Region').cumcount() + 1
foo = foo.pivot_table(index = ['Region','Sources'],columns = 'idx',values = ['Indicator','Status'],aggfunc = 'first')
foo = foo.sort_index(axis = 1,level = 1)
foo.columns = [f'{x}_{y}' for x,y in foo.columns]
foo = foo.reset_index()
但我没有收到任何有意义的输出,数据帧保持不变。
我错过了什么?谢谢!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)