dataframerustseriesrust-polars

Reading a specific value based on an indexed dataframe in rust with polars


I am trying to read a value from a dataframe based on index and column on rust. I do not know how to create an indexed dataframe. I have been unable to find much information on this. For instance, if my df was:

┌─────┬─────┬─────┬─────┬─────┐
│ ID  ┆ A   ┆ B   ┆ C   ┆ D   │
│ --- ┆ --- ┆ --- ┆ --- ┆ --- │
│ f64 ┆ f64 ┆ f64 ┆ f64 ┆ f64 │
╞═════╪═════╪═════╪═════╪═════╡
│ 0.0 ┆ 1.0 ┆ 2.0 ┆ 1.0 ┆ 1.0 │
│ 1.0 ┆ 2.0 ┆ 3.0 ┆ 1.0 ┆ 2.0 │
└─────┴─────┴─────┴─────┴─────┘

I would want to select a value for A based on the ID.

I have tried to read the column, convert it to a series then a vector and read based on index, but have been struggling to find working documentation. I have also tried based on row and then to series etc.

Any and all help is appreciated.


Solution

  • Using Polars expressions:

    let value = df
        .lazy()
        .filter(col("ID").eq(0.))
        .select([col("A")])
        .collect()
        .unwrap()["A"]
        .f64()
        .unwrap()
        .get(0)
        .unwrap();
    

    There are many other filters available.