Excel is known for pivot tables, but did you know that you can create pivot tables in polars too? That’s how versatile polars is. Below is a dataframe of students and their scores on different exams.
shape: (16, 3)
Student
Exam
Grade
str
str
i64
"Spencer"
"Mid-term"
93
"Spencer"
"Final"
88
"Emily"
"Mid-term"
73
"Emily"
"Final"
69
"Hannah"
"Mid-term"
55
…
…
…
"Emily"
"Final"
69
"Hannah"
"Mid-term"
55
"Hannah"
"Final"
89
"Aria"
"Mid-term"
92
"Aria"
"Final"
81
Pivot the dataframe
Right now, the dataframe is in long format, and you can see student names repeating. You can pivot the dataframe so that each student’s name appears only once, with the exam types as column names. Here’s how to do it: