I want to learn how to pivot data in Python like in Excel
Here is the data:
import pandas as pd `df = pd.DataFrame({'country': ['Angola', 'India', 'Mongolia'],1999: [800, 900, 1000],2000: [800, 878, 900],2001: [500, 345, 234],2002: [400, 555, 666]})
I want to pivot this dataset to have the country column appear vertically (i.e. see number of cases in each country each year)
Here is the code I am using:
df_melted = print(pd.melt(df, id_vars=['country'], var_name='year', value_name='value'))
Is this done correctly?