This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pub fn deal_with_columns<P: AsRef<Path>>(path: P) -> () { | |
/* Examples to deal with column and column names and enumerate */ | |
let df = read_csv(&path).unwrap(); | |
// column functions | |
let columns = df.get_columns(); // you can do for column in columns{} | |
let columname = df.get_column_names(); | |
// example like Python for i, val in enumerate(list, 0): | |
for (i, column) in columns.iter().enumerate(){ | |
println!("{}, {}", column, columname[i]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pub fn deal_with_stacks<P: AsRef<Path>>(path: P) -> () { | |
/* Stack, often happens to stack multiple dataframes together*/ | |
println!("Read the same dataframe twice"); | |
let df = read_csv(&path).unwrap(); | |
let df2 = read_csv(&path).unwrap(); | |
println!("Vertical stac the two dataframes"); | |
let mut df3 = df.vstack(&df2).unwrap(); // mut --> so we can change this dataframe later | |
println!("{}, {:#?}", df3.head(Some(5)), df3.shape()); | |
// get column | |
println!("Get a column"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pub fn deal_with_stacks<P: AsRef<Path>>(path: P) -> () { | |
/* Stack, often happens to stack multiple dataframes together*/ | |
println!("Read the same dataframe twice"); | |
let df = read_csv(&path).unwrap(); | |
let df2 = read_csv(&path).unwrap(); | |
println!("Vertical stack the two dataframes"); | |
let mut df3 = df.vstack(&df2).unwrap(); // mut --> so we can change this dataframe later | |
println!("{}, {:#?}", df3.head(Some(5)), df3.shape()); | |
// get column | |
println!("Get a column"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pub fn deal_with_stacks<P: AsRef<Path>>(path: P) -> () { | |
/* Stack, often happens to stack multiple dataframes together*/ | |
println!("Read the same dataframe twice"); | |
let df = read_csv(&path).unwrap(); | |
let df2 = read_csv(&path).unwrap(); | |
println!("Vertical stack the two dataframes"); | |
let mut df3 = df.vstack(&df2).unwrap(); // mut --> so we can change this dataframe later | |
println!("{}, {:#?}", df3.head(Some(5)), df3.shape()); | |
// get column | |
println!("Get a column"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Return a series | |
fn numb_to_log(in_df: &mut DataFrame) -> PolarResult<Series>{ | |
// unwrap to have Series, then .f64().uwrap() to retrieve a chunked array | |
let to_log10_column = in_df.drop_in_place("sepal.length").unwrap().rename("log10.sepal.length") | |
.f64().unwrap() // create chunked array | |
.cast::<Float64Type>() // here we have apply | |
.unwrap() // unwrap because we have Result<> | |
.apply(|s| s.log10()); | |
let series10 = to_log10_column.into_series(); // reconvert into a series |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let ifile = "PATHDOFILE/2d_array.csv"; | |
let df = read_csv(&ifile).unwrap(); | |
let ndarray = df.to_ndarray::<Float64Type>().unwrap(); | |
println!("{:?}", ndarray); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[package] | |
name = "polar_learning" | |
version = "0.1.0" | |
authors = ["Steboss89"] | |
edition = "2018" | |
[dependencies] | |
polars = "0.14.2" | |
polars-core = {version = "0.14.2", features=["ndarray"]} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn main() { | |
// Read input data | |
let ifile = "Boston.csv"; | |
// read and return a dataframe | |
let df = read_csv(&ifile).unwrap(); | |
// size and shape | |
let height = df.height(); | |
let cols = df.width(); | |
// select features |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[package] | |
name = "polar_smartcore" | |
version = "0.1.0" | |
authors = ["Steboss89 <[email protected]>"] | |
edition = "2018" | |
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | |
[dependencies] | |
polars = "0.14.2" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pub fn read_csv<P: AsRef<Path>>(path: P) -> PolarResult<DataFrame> { | |
/* Example function to create a dataframe from an input csv file*/ | |
let file = File::open(path).expect("Cannot open file."); | |
CsvReader::new(file) | |
.has_header(true) | |
//.with_delimiter(b' ') | |
//NB b" " throws an error | |
.finish() | |
} |