Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 13891

Pivot like Excel in Python

$
0
0

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?


Viewing all articles
Browse latest Browse all 13891

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>