I am trying to reproduce a solution from this answer, but make a mistake somewhere.
I have a dataframe with multiple columns:
'Region', 'Region code', 'Subregion', 'Country', 'Country code',
'IDA Status', 'FCV Status', 'Landlocked Status', 'Pillar', 'Indicator',
'Year', 'Value', 'Status', 'Measurement', 'Updated', 'Sources'
I need to convert two long columns into a wide format:
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
I want to make it from 2 to 9 columns
Indicator1_Actual | Indicator1_Forecast | Indicator1_Target | Indicator2_Actual
20 30 60 60
etc
I am doing the solution from the question above
foo['idx'] = foo.groupby('Region').cumcount() + 1
foo = foo.pivot_table(index = ['Region', 'Region code', 'Subregion', 'Country', 'Country code',
'IDA Status', 'FCV Status', 'Landlocked Status', 'Pillar','Indicator',
'Year', 'Value', 'Status', 'Measurement', 'Updated', '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()
But I do not receive any meaningful output, the dataframe remains the same.
What am I missing? Thanks!