Rust Polars: How to get the row count of a DataFrame?

I want to filter a Polars DataFrame and then get the number of rows.

What I'm doing now seems to work but feels so wrong:

    let item_count = item_df

In a subsequent DataFrame operation I need to use this in a division operation

                   .div(lit(item_count as f64))

This is for a tiny dataset (tens of rows) so I'm not worried about performance but I'd like to learn what the best way would be.


  • While there doesn't seem to be a predefined method on LazyFrame, you can use polars expressions:

    use polars::prelude::*;
    let df = df!["a" => [1, 2], "b" => [3, 4]].unwrap();

    And to get the numeric value: