Tidying variable values as column names with melt
Like most large Python libraries, pandas has many different ways to accomplish the same task--the differences usually being readability and performance. Pandas contains a DataFrame method named melt
that works similarly to the stack
method described in the previous recipe but gives a bit more flexibility.
Note
Before pandas version 0.20, melt
was only provided as a function that had to be accessed with pd.melt
. Pandas is still an evolving library and you need to expect changes with each new version. Pandas has been making a push to move all functions that only operate on DataFrames to methods, such as they did with melt
. This is the preferred way to use melt
and the way this recipe uses it. Check the What's New part of the pandas documentation to stay up to date with all the changes (http://bit.ly/2xzXIhG).
Getting ready
In this recipe, we use the melt
method to tidy a simple DataFrame with variable values as column names.
Â
Â
Â
How to do it...
- Read...