Skip to content

Instantly share code, notes, and snippets.

View Steboss89's full-sized avatar

Stefano Bosisio Steboss89

View GitHub Profile
@Steboss89
Steboss89 / check_cols.rs
Created June 28, 2021 14:49
Check a dataframe column
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]);
@Steboss89
Steboss89 / stack_dfs.rs
Created June 28, 2021 14:51
Stack dataframes together
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");
@Steboss89
Steboss89 / stack_dfs.rs
Created June 29, 2021 08:05
Vertical and horizontal stacking in Rust
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");
@Steboss89
Steboss89 / stack_df.rs
Created June 29, 2021 08:06
Vertical and Horizontal stacking
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");
@Steboss89
Steboss89 / operations_on_series2.rs
Created June 29, 2021 08:21
log10transform on series
// 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
@Steboss89
Steboss89 / convert_df_ndarray.rs
Created June 29, 2021 09:46
Use polars feature ndarray
let ifile = "PATHDOFILE/2d_array.csv";
let df = read_csv(&ifile).unwrap();
let ndarray = df.to_ndarray::<Float64Type>().unwrap();
println!("{:?}", ndarray);
@Steboss89
Steboss89 / Cargo.toml
Created June 29, 2021 09:50
Use features
[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"]}
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
@Steboss89
Steboss89 / Cargo.toml
Created June 30, 2021 11:41
prepare cargo for smartcore
[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"
@Steboss89
Steboss89 / main.rs
Created June 30, 2021 14:43
read csv and extract features
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()
}