Pivot Longer
In this lesson we will learn the opposite transformation to
what we learned in the previous lesson.
pivot
transformed our data from long
to wide. melt
will do the opposite,
transform our data from wide to long.
As a recap, let's review the code introduced in the previous lesson when we converted the iris dataset from long to wide.
Next we will transform the data back from wide to long.
The way to do this is actually quite similar to the way we
transformed the data from long to wide. We will use the
melt
method, which takes as arguments:
- id_vars: This parameter specifies the column to be kept as the identifying column, in this case rows.
- value_vars: This parameter specifies the columns to be moved from wide to long, in this case a list of setosa, versicolor, and virginica.
- var_name: This parameter specifies the name to give to the new column that will be created to hold the group variable, in this case Species.
- value_name: This parameter specifies the name to give to the new column that will be created to hold the values, in this case Petal.Length
To use the melt
method we will need the
rows column to be brought in from the
index column into the actual dataframe. To do that
we will use the reset_index
method
on the dataframe.
Then we call the melt
method on this
dataframe, passing in the arguments as described above.
Inspecting the data, we see that we have successfully
transformed the data back to its original long form
- although omitting the Petal.Length, Sepal.Length
and Sepal.Width columns that were dropped
in the transformation.
We could keep those, but that problem is better kept for
an advanced course in data science.
Note that in the code block on the right, we have included
the iris_wide
code from the previous prompt for
you, so that does not need to be re-entered.
Practice exercise
This exercise continues the exercise from the previous lesson,
so you can use your solution from there as the starting point.
Use assign
, loc
and pivot
to transform the iris dataset from long to wide filling in the
values from Petal.Width. Next map the data back to long form
using melt
.