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!

1
  • 1
    df.melt Commented Dec 1, 2021 at 12:52

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much! Easier than I thought..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.