Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/docs/source/src/rust/user-guide/transformations/concatenation.rs
7889 views
1
// --8<-- [start:setup]
2
use polars::prelude::*;
3
// --8<-- [end:setup]
4
5
fn main() -> Result<(), Box<dyn std::error::Error>> {
6
// --8<-- [start:vertical]
7
let df_v1 = df!(
8
"a"=> &[1],
9
"b"=> &[3],
10
)?;
11
let df_v2 = df!(
12
"a"=> &[2],
13
"b"=> &[4],
14
)?;
15
let df_vertical_concat =
16
concat([df_v1.lazy(), df_v2.lazy()], UnionArgs::default())?.collect()?;
17
println!("{}", &df_vertical_concat);
18
// --8<-- [end:vertical]
19
20
// --8<-- [start:horizontal]
21
let df_h1 = df!(
22
"l1"=> &[1, 2],
23
"l2"=> &[3, 4],
24
)?;
25
let df_h2 = df!(
26
"r1"=> &[5, 6],
27
"r2"=> &[7, 8],
28
"r3"=> &[9, 10],
29
)?;
30
let df_horizontal_concat =
31
polars::functions::concat_df_horizontal(&[df_h1, df_h2], true, false)?;
32
println!("{}", &df_horizontal_concat);
33
// --8<-- [end:horizontal]
34
//
35
// --8<-- [start:horizontal_different_lengths]
36
let df_h1 = df!(
37
"l1"=> &[1, 2],
38
"l2"=> &[3, 4],
39
)?;
40
let df_h2 = df!(
41
"r1"=> &[5, 6, 7],
42
"r2"=> &[8, 9, 10],
43
)?;
44
let df_horizontal_concat =
45
polars::functions::concat_df_horizontal(&[df_h1, df_h2], true, false)?;
46
println!("{}", &df_horizontal_concat);
47
// --8<-- [end:horizontal_different_lengths]
48
49
// --8<-- [start:cross]
50
let df_d1 = df!(
51
"a"=> &[1],
52
"b"=> &[3],
53
)?;
54
let df_d2 = df!(
55
"a"=> &[2],
56
"d"=> &[4],)?;
57
let df_diagonal_concat = polars::functions::concat_df_diagonal(&[df_d1, df_d2])?;
58
println!("{}", &df_diagonal_concat);
59
// --8<-- [end:cross]
60
Ok(())
61
}
62
63