How do I fix this pandas reshaping attempt

37 Views Asked by At

My aim is to get a single line of data for each country and year within the data frame, this means I need to get the indicator which comes under the Series Name variable on the column axis.

I've attempted both melt() and pivoting to get the desired output but the furthest I got can be seen below.

WGI_LONG = pd.melt(WGI_DF, id_vars=['Country Name', 'Country Code', 'Series Name', 'Series Code'],
                       var_name='Year', value_name='Value')

SPI_LONG = pd.melt(SPI_DF, id_vars=['Country Name', 'Country Code', 'Series Name', 'Series Code'],
                       var_name='Year', value_name='Value')

WGI_PIVOTED = WGI_LONG.pivot_table(index=['Country Name', 'Country Code', 'Year'], columns='Series Name', values='Value')

Output from Long Output from Pivot

0

There are 0 best solutions below