This example uses Rust Polars version = "0.30"
Apply the same function on the selected columns:
lazyframe
.with_columns([
cols(col_name1, col_name2, ..., col_nameN)
.apply(|series|
some_function(series),
GetOutput::from_type(DataType::Float64)
)
]);
Or apply many functions to many columns with a much more flexible and powerful method:
lazyframe
.with_columns([
some_function1(col_name1, col_name2, ..., col_nameN),
some_function2(col_name1, col_name2, ..., col_nameM),
some_functionN(col_name1, col_name2, ..., col_nameZ),
]);
The Cargo.toml:
[dependencies]
polars = { version = "0.30", features = [
"lazy", # Lazy API
"round_series", # round underlying float types of Series
] }
And the main() function:
use std::error::Error;
use polars::{
prelude::*,
datatypes::DataType,
};
fn main()-> Result<(), Box<dyn Error>> {
let dataframe01: DataFrame = df!(
"integers" => &[1, 2, 3, 4, 5, 6],
"float64 A" => [23.654, 0.319, 10.0049, 89.01999, -3.41501, 52.0766],
"options" => [Some(28), Some(300), None, Some(2), Some(-30), None],
"float64 B" => [9.9999, 0.399, 10.0061, 89.0105, -3.4331, 52.099999],
)?;
println!("dataframe01: {dataframe01}\n");
// let selected: Vec<&str> = vec!["float64 A", "float64 B"];
// Example 1:
// Format only the columns with float64
// input: two columns --> output: two columns
let lazyframe: LazyFrame = dataframe01
.lazy()
.with_columns([
//cols(selected)
all()
.apply(|series|
round_float64_columns(series, 2),
GetOutput::same_type()
)
]);
let dataframe02: DataFrame = lazyframe.clone().collect()?;
println!("dataframe02: {dataframe02}\n");
let series_a: Series = Series::new("float64 A", &[23.65, 0.32, 10.00, 89.02, -3.42, 52.08]);
let series_b: Series = Series::new("float64 B", &[10.00, 0.4, 10.01, 89.01, -3.43, 52.1]);
assert_eq!(dataframe02.column("float64 A")?, &series_a);
assert_eq!(dataframe02.column("float64 B")?, &series_b);
// Example 2:
// input1: two columns --> output: one new column
// input2: one column --> output: one new column
// input3: two columns --> output: one new column
let lazyframe: LazyFrame = lazyframe
.with_columns([
apuracao1("float64 A", "float64 B", "New Column 1"),
apuracao2("float64 A", "New Column 2"),
(col("integers") * lit(10) + col("options")).alias("New Column 3"),
]);
println!("dataframe03: {}\n", lazyframe.collect()?);
Ok(())
}
pub fn round_float64_columns(series: Series, decimals: u32) -> Result<Option<Series>, PolarsError> {
match series.dtype() {
DataType::Float64 => Ok(Some(series.round(decimals)?)),
_ => Ok(Some(series))
}
}
fn apuracao1(name_a: &str, name_b: &str, new: &str) -> Expr {
(col(name_a) * col(name_b) / lit(100))
//.over("some_group")
.alias(new)
}
fn apuracao2(name_a: &str, new: &str) -> Expr {
(lit(10) * col(name_a) - lit(2))
//.over("some_group")
.alias(new)
}
The output:
dataframe01: shape: (6, 4)
┌──────────┬───────────┬─────────┬───────────┐
│ integers ┆ float64 A ┆ options ┆ float64 B │
│ --- ┆ --- ┆ --- ┆ --- │
│ i32 ┆ f64 ┆ i32 ┆ f64 │
╞══════════╪═══════════╪═════════╪═══════════╡
│ 1 ┆ 23.654 ┆ 28 ┆ 9.9999 │
│ 2 ┆ 0.319 ┆ 300 ┆ 0.399 │
│ 3 ┆ 10.0049 ┆ null ┆ 10.0061 │
│ 4 ┆ 89.01999 ┆ 2 ┆ 89.0105 │
│ 5 ┆ -3.41501 ┆ -30 ┆ -3.4331 │
│ 6 ┆ 52.0766 ┆ null ┆ 52.099999 │
└──────────┴───────────┴─────────┴───────────┘
dataframe02: shape: (6, 4)
┌──────────┬───────────┬─────────┬───────────┐
│ integers ┆ float64 A ┆ options ┆ float64 B │
│ --- ┆ --- ┆ --- ┆ --- │
│ i32 ┆ f64 ┆ i32 ┆ f64 │
╞══════════╪═══════════╪═════════╪═══════════╡
│ 1 ┆ 23.65 ┆ 28 ┆ 10.0 │
│ 2 ┆ 0.32 ┆ 300 ┆ 0.4 │
│ 3 ┆ 10.0 ┆ null ┆ 10.01 │
│ 4 ┆ 89.02 ┆ 2 ┆ 89.01 │
│ 5 ┆ -3.42 ┆ -30 ┆ -3.43 │
│ 6 ┆ 52.08 ┆ null ┆ 52.1 │
└──────────┴───────────┴─────────┴───────────┘
dataframe03: shape: (6, 7)
┌──────────┬───────────┬─────────┬───────────┬──────────────┬──────────────┬──────────────┐
│ integers ┆ float64 A ┆ options ┆ float64 B ┆ New Column 1 ┆ New Column 2 ┆ New Column 3 │
│ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- │
│ i32 ┆ f64 ┆ i32 ┆ f64 ┆ f64 ┆ f64 ┆ i32 │
╞══════════╪═══════════╪═════════╪═══════════╪══════════════╪══════════════╪══════════════╡
│ 1 ┆ 23.65 ┆ 28 ┆ 10.0 ┆ 2.365 ┆ 234.5 ┆ 38 │
│ 2 ┆ 0.32 ┆ 300 ┆ 0.4 ┆ 0.00128 ┆ 1.2 ┆ 320 │
│ 3 ┆ 10.0 ┆ null ┆ 10.01 ┆ 1.001 ┆ 98.0 ┆ null │
│ 4 ┆ 89.02 ┆ 2 ┆ 89.01 ┆ 79.236702 ┆ 888.2 ┆ 42 │
│ 5 ┆ -3.42 ┆ -30 ┆ -3.43 ┆ 0.117306 ┆ -36.2 ┆ 20 │
│ 6 ┆ 52.08 ┆ null ┆ 52.1 ┆ 27.13368 ┆ 518.8 ┆ null │
└──────────┴───────────┴─────────┴───────────┴──────────────┴──────────────┴──────────────┘