Skip to content

Instantly share code, notes, and snippets.

@tkb
Last active December 20, 2015 13:09
Show Gist options
  • Save tkb/6136929 to your computer and use it in GitHub Desktop.
Save tkb/6136929 to your computer and use it in GitHub Desktop.
import pandas as pd
#read the normalized CSV file
df = pandas.read_csv('lifeexpectancy.csv')
#melt the normalized file, hold the country name and code variables, rename the melted columns
le = pd.melt(df, id_vars=['Country Name','Country Code'], var_name="year", value_name="life_expectancy")
#sort by country name for convenience
le2 = le.sort(['Country Name'])
#write out the csv without and index
le2.to_csv('lifeexpectancy-unpivoted.csv', sep=',', index=False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment