Reshaping with pd.DataFrame.pivot and pd.pivot_table
So far in this chapter, we have seen that pd.DataFrame.stack
, pd.DataFrame.melt
, and pd.wide_to_long
can all be used to help you convert your pd.DataFrame
from a wide to a long format. On the flip side, we have only seen pd.Series.unstack
helps us go from long to wide, but that method has the downside of requiring us to assign a proper row index before we can use it. With pd.DataFrame.pivot
, you can skip any intermediate steps and go directly from a long to a wide format.
Beyond pd.DataFrame.pivot
, pandas offers a pd.pivot_table
function, which can not only reshape from long to wide but allows you to perform aggregations as part of the reshape.
Figure 7.6: Using pd.pivot_table to reshape with sum aggregation
Effective use of pd.pivot_table
allows you to perform very complex calculations with a compact and concise syntax.
How to do it
In many of the preceding recipes, we have started with data in wide form...