Say you have a target you want to hit for your weekly sales total, and you also want to know how close you are to hitting that target with each passing day. It turns out there’s a way to perform that calculation in Polars. Below is a dataframe containing daily sales.
shape: (7, 2)
Day
Sales
str
i64
"Mon"
182
"Tue"
247
"Wed"
547
"Thur"
481
"Fri"
319
"Sat"
208
"Sun"
67
Calculate cumulative sum
In Polars, you use the expression pl.cum_sum to add the next number to the sum of the previous numbers. Here’s how to do it: