Home > other >  Pandas make columns to rows
Pandas make columns to rows

Time:12-11

I got a pandas dataframe which looks like the following picture:

enter image description here

Every year is a new column, but i want them in one column called year.

It should look similar like this dataframe:

enter image description here

Anybody got an idea, how i can achieve this? Thanks!

CodePudding user response:

you need to use the melt method, something like this

df2 = df.melt(id_vars=['country','continent'], var_name="year", value_vars=[str(x) for x in range(1850,2011)])
  • Related