How it works...

The pivot_table method is very versatile and flexible but performs a rather similar operation to a groupby aggregation with step 1 showing a simple example. The index parameter takes a column (or columns) that will not be pivoted and whose unique values will be placed in the index. The columns parameter takes a column (or columns) that will be pivoted and whose unique values will be made into column names. The values parameter takes a column (or columns) that will be aggregated.

There also exists an aggfunc parameter that takes an aggregating function (or functions) that determines how the columns in the values parameter get aggregated. It defaults to the mean, and, in this example, we change it to calculate the sum. Additionally, some unique combinations of AIRLINE and ORG_AIR do not exist. These missing combinations will default to missing values in the resulting DataFrame. Here, we use the fill_value parameter to change them to zero.

Step 2 begins the replication process using all the columns in the index and columns parameter as the grouping columns. This is the key to making this recipe work. A pivot table is simply an intersection of all the unique combinations of the grouping columns. Step 3 finishes the replication by pivoting the innermost index level into column names with the unstack method. Just like with pivot_table, not all combinations of AIRLINE and ORG_AIR exist; we again use the fill_value parameter to force these missing intersections to zero.

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

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