I have a DataFrame that has the following columns: 'Series Name', 'Country Name', '2000', '20001' , ..., '2018'. In 'Series Name' I have the variables that I want to analize (for example, 'PBI', 'Surface Area', etc). So, in 'Series Name' I have repeated values (PBI for each Country, for example).
| Series Name | Country Name | 2000 [YR2000] | 2001 [YR2001] |
|---|---|---|---|
| Superficie (kilómetros cuadrados) | Estados Unidos | 9632030 | 9632030 |
| Superficie (kilómetros cuadrados) | Unión Europea | 4384964,995 | 4384984,995 |
| Superficie (kilómetros cuadrados) | Japón | 377800 | 377880 |
I want to reorder the DataFrame in this way: - Columns: 'Country Name', 'Year' and all the unique values of 'Series Name' - Year column: take the the values from 2000 to 2018 for each Country - 'Series Name' columns : each column ('PBI', 'Surface' etc) has the value for the respective Year and Country.
| Country Name | Year | Superficie (kilómetros cuadrados) |
|---|---|---|
| Estados Unidos | 2000 | 9632030 |
| Estados Unidos | 2001 | 9632030 |
| Unión Europea | 2000 | 4384964,995 |
| Unión Europea | 2001 | 4384964,995 |
| Japón | 2000 | 377800 |
| Japón | 2001 | 377800 |
I tried with melt, but I really don´t know how to use it:
indicadores_clean_pv = pd.melt(indicadores_clean, id_vars=['Country Name'], var_name='Año')
I´ve also tried :
indicadores_clean_pv.melt(id_vars='Country Name', var_name='Year').groupby(['Country Name','Year']).agg({'Series Name':'first'}).reset_index()
But I can't put the 'Series Name' values in the columns
Can anyone help me? Thanks in advance!
You can use
pivot_table:Output: