Sometimes you may want to get the index positions of a repeating value in a Polars dataframe. Below is a dataframe showing students and their grades.
shape: (20, 2)
| str |
i64 |
| "Spencer" |
93 |
| "Spencer" |
88 |
| "Emily" |
73 |
| "Emily" |
69 |
| "Hannah" |
55 |
| … |
… |
| "Hannah" |
89 |
| "Aria" |
92 |
| "Aria" |
81 |
| "Mona" |
97 |
| "Paige" |
71 |
Get index of rows
To get the index positions of rows where the name “Aria” appears, use the Polars expression arg_true like this:
(df
.select((pl.col('Student') == 'Aria').arg_true())
)
Check out my Polars course to advance your data analysis skills.