In our previous post, we looked at how to pivot in pandas. In this post, we will look at how to unpivot data in pandas using pandas.melt() function.
Sometimes the data that you have contains values in the columns instead of variables. And you want to melt or unpivot the wide form data to long form. To do that you can use the pd.melt() function in pandas.
Let’s read a data to work with before moving further.
import pandas as pd import plotly.express as px wide = px.data.medals_wide() wide
Here, we have some data about Olympics medals by country.
Now to melt or unpivot this data you can use the pandas pd.melt() function.
It has two parameters –
id_vars – All the columns passed to id_vars will remain as it is.
Value_vars – All the columns passed to value_vars gets melt down (unpivot). By default it will unpivot all the columns that is not specified in the id_vars column.
To unpivot the above table, you have to write –
# keep nation as it is pd.melt(wide, id_vars='nation')
Here, we kept the nation column as it is and melted all the other columns. If you look at the above result you can see that by default pandas named the new column as variable and data as value. If you want you can rename them using the var_name and value_name parameter in melt.
pd.melt(wide, id_vars='nation', var_name='Medals', value_name='Count')
Sometimes you may want to keep more than one column as it is and melt the rest of the columns. To demonstrate that let’s read a dataset.
wide_df = pd.read_csv("https://raw.githubusercontent.com/bprasad26/lwd/master/data/billboard.csv") wide_df.head()
Here, we have billboard data and we want to melt all the week chart data and keep the rest of the columns as it is. To do that you have to write
pd.melt(wide_df, id_vars=['year','artist','track','time','date.entered'], var_name='week')