
In data analysis, you’re constantly reshaping data. we used melt() to turn “wide” data into “long” data.
Today, we’re doing the opposite. pivot() is the Polars function that turns “long” data into a “wide” data (a pivot table).
The Data
We’ll start with Long Data, which is good for analysis:
import polars as pl
df_long = pl.DataFrame({
"student": ["Alice", "Alice", "Bob", "Bob"],
"exam": ["Math", "History", "Math", "History"],
"score": [90, 85, 80, 95]
})
print(df_long)shape: (4, 3) โโโโโโโโโโโฌโโโโโโโโโโฌโโโโโโโโ โ student โ exam โ score โ โ --- โ --- โ --- โ โ str โ str โ i64 โ โโโโโโโโโโโชโโโโโโโโโโชโโโโโโโโก โ Alice โ Math โ 90 โ โ Alice โ History โ 85 โ โ Bob โ Math โ 80 โ โ Bob โ History โ 95 โ โโโโโโโโโโโดโโโโโโโโโโดโโโโโโโโ
This is hard to read. We want a “wide” table, with students as rows and exams as columns.
The pivot() Method
pivot() is an all-in-one operation:
index: The column to use for rows.on: The column to “pivot” into new columns.values: The column to use for the cell values.
df_wide = df_long.pivot(
index="student",
on="exam",
values="score"
)
print(df_wide)Output (Wide Data):
shape: (2, 3) โโโโโโโโโโโฌโโโโโโโโโโฌโโโโโโโ โ student โ History โ Math โ โ --- โ --- โ --- โ โ str โ i64 โ i64 โ โโโโโโโโโโโชโโโโโโโโโโชโโโโโโโก โ Alice โ 85 โ 90 โ โ Bob โ 95 โ 80 โ โโโโโโโโโโโดโโโโโโโโโโดโโโโโโโ
This is the high-performance Polars way to create a pivot table, perfect for generating human-readable reports from your tidy “long” data.
Key Takeaways
- The article explains how to reshape data in analysis using the Polars library.
- First, it discusses converting ‘wide’ data into ‘long’ data with the melt() function.
- Then, it introduces the Polars pivot function, which transforms ‘long’ data back into ‘wide’ format.
- The pivot() method requires three arguments: index, on, and values, allowing for flexible data manipulation.
- Finally, the output creates a pivot table, making the data more readable for analysis.





