shape: (4, 2)
| Customer | Receipt No |
|---|---|
| str | i64 |
| "Serena" | 1 |
| "Blair" | 2 |
| "Dan" | 21 |
| "Vanessa" | 153 |
zfill
Joram Mutenge
2025-09-21
You have a shop and want your receipt numbers to have four digits instead of just one. So rather than receipt “No. 1”, you want receipt “No. 0001”. Below is a dataframe showing customers and their receipt numbers.
| Customer | Receipt No |
|---|---|
| str | i64 |
| "Serena" | 1 |
| "Blair" | 2 |
| "Dan" | 21 |
| "Vanessa" | 153 |
To add zeros at the start of values in Receipt No, you first need to convert the column’s data type to string (text) and then use the Polars expression zfill, like this:
| Customer | Receipt No |
|---|---|
| str | str |
| "Serena" | "0001" |
| "Blair" | "0002" |
| "Dan" | "0021" |
| "Vanessa" | "0153" |
Setting length=4 means the final value will have 4 digits. It does not mean the number of zeros to add.
My Polars course is newly updated. Enroll now to advance your data analysis skills.