How it works...

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 method preserves the column name used in the columns parameter as the name of the column index. After resetting the index, this name is meaningless, and we remove it with rename_axis.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.237.123