How it works...

It is common for the result of a groupby aggregation to produce a DataFrame or Series with multiple axis levels. The resulting DataFrame from the groupby operation in step 1 has multiple levels for each axis. The column levels are not named, which would require us to reference them only by their integer location. To greatly ease our ability to reference the column levels, we rename them with the rename_axis method.

The rename_axis method is a bit strange in that it can modify both the level names and the level values based on the type of the first argument passed to it. Passing it a list (or a scalar if there is only one level) changes the names of the levels. Passing it a dictionary or a function changes the values of the levels. In step 2, we pass the rename_axis method a list and are returned a DataFrame with all axis levels named.

Once all the axis levels have names, we can easily and explicitly control the structure of data. Step 3 stacks the AGG_FUNCS column into the innermost index level. The swaplevel method in step 4 accepts the name or position of the levels that you want to swap as the first two arguments. The sort_index method is called twice and sorts the actual values of each level. Notice that the values of the column level are the column names SATMTMID and UGDS.

We can get vastly different output by both stacking and unstacking, as done in step 6. It is also possible to stack every single column level into the index to produce a Series.

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

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