Search code examples
dataframerustrust-polars

How to split a polars DataFrame into several dataframes in rust?


I use Polars to read from a CSV file. I want to separate the DataFrame into several DataFrames by the "date" column (I want to analyze the data by day).

I tried to use partition_by but it seems not to be implemented (I got the error: "no method named partition_by found for mutable reference &mut DataFrame in the current scope method not found in &mut DataFrame").


Solution

  • does implement partition_by, but you need to enable the partition_by feature in your Cargo.toml file.

    [dependencies]
    polars = { version = "0.39.2", features = ["lazy", "partition_by"] }
    

    Example:

    use polars::prelude::*;
    
    fn main() {
        let df = df! (
            "a" => &["a", "b", "c", "a"],
            "b" => &[1, 2, 3, 2]
        )
        .unwrap();
        let dfs = df.partition_by(&["a"], true);
        println!("{:?}", dfs.unwrap());
    }