Created
June 29, 2021 08:21
-
-
Save Steboss89/a0aeac56b5e74dbcb15f8059e48eddbe to your computer and use it in GitHub Desktop.
log10transform on series
This file contains hidden or 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 | |
// return the column | |
println!("{}", series10); | |
Ok(series10) | |
} | |
pub fn deal_with_apply<P: AsRef<Path>>(path: P) -> () { | |
/* Apply is one of the key functions in pandas*/ | |
let mut df = read_csv(&path).unwrap(); | |
// apply a closure | |
println!("Add 1 to first column"); | |
df.apply_at_idx(0, |s| s+1); | |
println!("{}", df.head(Some(5))); | |
// compute the log transform of a column and learn to play with series and chunked arrays | |
let log10_series = numb_to_log(&mut df); | |
// insert the log10-column | |
println!(" log 10 of sepal length"); | |
df.with_column(log10_series.unwrap()); | |
println!("{}", df.head(Some(5))); | |
// let's see how to perform the log10 transform directly from teh dataframe | |
df.apply_at_idx(0, |s| s.f64().unwrap().apply(|t| t.log10())); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment