Created
June 28, 2021 14:51
-
-
Save Steboss89/4635634d91f7d3a8980c6e299619aa76 to your computer and use it in GitHub Desktop.
Stack dataframes together
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
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"); | |
let sepal_length = df3.column("sepal.length").unwrap(); | |
println!("{}", sepal_length); | |
println!("{:#?}", sepal_length.len()); | |
// drop columns | |
println!("Drop a column"); | |
let sepal_length = df3.drop_in_place("sepal.length").unwrap(); // inplace | |
// this commands return a Series | |
println!("{}", df3.head(Some(5))); | |
// drop_nulls() to drop NaN | |
//let df4 = df3.drop("sepal.length"); // if we don't want a mut dataframe df3 | |
println!("Insert a series in a dataframe as a new column"); | |
let _df4 = df3.insert_at_idx(0, sepal_length).unwrap(); | |
println!("{}", _df4.head(Some(5))); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment