Skip to content

Instantly share code, notes, and snippets.

@Steboss89
Created June 29, 2021 08:06
Show Gist options
  • Save Steboss89/d048551f9c85d3db937b8c94956f1f14 to your computer and use it in GitHub Desktop.
Save Steboss89/d048551f9c85d3db937b8c94956f1f14 to your computer and use it in GitHub Desktop.
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");
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