I have the following dataframe
use polars::prelude::*;
fn main() {
let df = df!("1" => &[1, 2, 3, 4, 5,6], "2" => &[2, 3, 4, 5, 6, 6]);
println!("{}", df.unwrap());
}
shape: (6, 2)
┌─────┬─────┐
│ 1 ┆ 2 │
│ --- ┆ --- │
│ i32 ┆ i32 │
╞═════╪═════╡
│ 1 ┆ 2 │
│ 2 ┆ 3 │
│ 3 ┆ 4 │
│ 4 ┆ 5 │
│ 5 ┆ 6 │
│ 6 ┆ 6 │
└─────┴─────┘
How can I retrieve the number of columns in df(In this case it's 2)?
There are multiple ways to do this.
.widthmethod which returns the number of columns.get_column_names()to get the column names as vector, and then call it'slenmethod..shapemethod to get the tuple(no_of_rows, no_of_columns)and access the second element.