Turning rows into columns
The pivot operation works the opposite way from how the unpivot and unpivot operations work. It turns rows into columns or converts your data from a long to a wide format. A wide format is often easier to read and understand. Also, when conducting statistical analyses such as regression analysis or building a predictive model, having each variable as individual columns is more convenient.
In this recipe, we’ll look at a few ways to turn rows into columns or convert a long format to a wide format.
Getting ready
In this recipe, we’ll start with the DataFrame in the long format as created in the last recipe. If you haven’t gone through that recipe, here’s the code you need to execute to create the DataFrame:
from polars import selectors as cs long_df = ( pl.read_csv('../data/academic.csv') .select( pl.col('year...