December 2018
Beginner to intermediate
682 pages
18h 1m
English
Once we have identified the variables in step 1, we can begin our restructuring. Pandas does not have a method to pivot columns simultaneously, so we must take on this task one step at a time. We correct the years by keeping the Property column vertical by passing it to the id_vars parameter in the melt method.
The result is now precisely the pattern of messy data found in the preceding recipe, Tidying when multiple variables are stored as column values. As explained in the There's more section of that recipe, we must use pivot_table to pivot a DataFrame when using more than one column in the index parameter. After pivoting, the Group and Year variables are stuck in the index. We push them back out as columns. The pivot_table ...