user1781186
user1781186

Reputation:

Turn columns to rows in pandas

I have a dataframe with the names of newborn babies per year.

"name","1998","1999","2000","2001","2002","2003","2004","2005","2006","2007","2008","2009","2010","2011","2012","2013"
"Aicha",0,0,0,0,0,0,0,0,0,0,0,0,10,0,0,0
"Aida",15,20,16,0,10,0,10,14,13,11,12,11,13,14,13,18
"Aina",0,0,0,0,0,0,16,12,15,13,12,14,10,11,0,12
"Aisha",14,0,10,12,15,13,28,33,26,26,52,44,43,54,68,80
"Ajla",15,10,0,0,22,18,28,27,26,26,19,16,19,22,17,27
"Alba",0,0,14,14,22,14,17,19,23,15,28,32,25,33,33,33

I want to plot this in a line chart, where each line is a different name and the x axis is the years. In order to do that, I imagine I need to reshape the data into something like this:

"name","year","value"
"Aicha","1998",0
"Aicha","1999",0
"Aicha","2000",0
...

How do I reshape the data in that fashion? I've tried pivot_table but I can't seem to get it to work.

Upvotes: 0

Views: 87

Answers (1)

DSM
DSM

Reputation: 353359

You could use pd.melt:

>>> df_melted = pd.melt(df, id_vars="name", var_name="year")
>>> df_melted.head()
    name  year  value
0  Aicha  1998      0
1   Aida  1998     15
2   Aina  1998      0
3  Aisha  1998     14
4   Ajla  1998     15

and then sort using

>>> df_melted = df_melted.sort("name")

if you liked.

Upvotes: 1

Related Questions