rustrust-polars

Rust Polars Dataframe String replacement


I am writing a Rust using Polars. I would like to know how can I manipulate one string dataframe column.

For example, I have the following dataframe:

Id Text
1 Some foo text
2 Other text

And I would like to replace all values that has "foo" by some other value like "new foo" and I wasn't able to find a way to do that. Can anyone help me on this?

I tried to use the function with_column, but I didn't manage how to do it.


Solution

  • You can do like the following:

    use polars::prelude::*;
    use polars::df;
    use regex::Regex;
    
    fn main() {
        // use macro
        let mut df = df! [
            "Id" => [1,2],
            "Text" => ["Some foo text", "Other text",],
        ].unwrap();
    
        println!("before = {:?}", df);
        
        let re = Regex::new(r"foo").unwrap();
        let target = "new foo";
    
        let new_foo_series = df.column("Text").unwrap()
            .utf8()
            .unwrap()
            .apply(|e| re.replace(e, target));
        
        let df2 = df.with_column(new_foo_series).unwrap();
        
        println!("after = {:?}", df2);
    
    }
    
    

    That will show:

    before = shape: (2, 2)
    ┌─────┬───────────────┐
    │ Id  ┆ Text          │
    │ --- ┆ ---           │
    │ i32 ┆ str           │
    ╞═════╪═══════════════╡
    │ 1   ┆ Some foo text │
    │ 2   ┆ Other text    │
    └─────┴───────────────┘
    after = shape: (2, 2)
    ┌─────┬───────────────────┐
    │ Id  ┆ Text              │
    │ --- ┆ ---               │
    │ i32 ┆ str               │
    ╞═════╪═══════════════════╡
    │ 1   ┆ Some new foo text │
    │ 2   ┆ Other text        │
    └─────┴───────────────────┘
    

    Ref = https://docs.rs/polars/latest/polars/docs/eager/index.html#apply-functions-closures