2

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!

Aiden Blake
  • 117
  • 5
  • 1
    [df.melt](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.melt.html#pandas.DataFrame.melt) – Emi OB Dec 01 '21 at 12:52

1 Answers1

1

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
Mohammadreza Riahi
  • 502
  • 1
  • 6
  • 20