Please can you help me? This is the structure of my origin dataset:
Country | 2020 | 2021 |
---|---|---|
Ecuador | Value1 | Value2 |
Canada | Value1 | Value2 |
And i would like to get this structure, so the year is a index itself and not multiple columns:
Country | Year | Index |
---|---|---|
Ecuador | 2020 | Value1 |
Ecuador | 2021 | Value2 |
Canada | 2020 | Value1 |
Canada | 2021 | Value2 |
Thank you very much!
CodePudding user response:
Your question is similar to this question. You can use the melt method of pandas
df.melt(id_vars=['Country'], var_name='Year', value_name='Index')
the output is:
Country Year Index
0 Ecuador 2020 Value1
1 Canada 2020 Value1
2 Ecuador 2021 Value2
3 Canada 2021 Value2