Concatenation

There are a number of ways to concatenate data from separate DataFrames:

  • two dataframes with the same columns can be vertically concatenated to make a longer dataframe
  • two dataframes with non-overlapping columns can be horizontally concatenated to make a wider dataframe
  • two dataframes with different numbers of rows and columns can be diagonally concatenated to make a dataframe which might be longer and/ or wider. Where column names overlap values will be vertically concatenated. Where column names do not overlap new rows and columns will be added. Missing values will be set as null

Vertical concatenation - getting longer

In a vertical concatenation you combine all of the rows from a list of DataFrames into a single longer DataFrame.

import nanuq

let dfV1 = newDataFrame:
  a: [1]
  b: [3]
let dfV2 = newDataFrame:
  a: [2]
  b: [4]
let dfVerticalConcat = concat([dfV1, dfV2], how = Vertical)
nbShow dfVerticalConcat
shape: (2, 2)
ab
i64i64
13
24

Vertical concatenation fails when the dataframes do not have the same column names.

Horizontal concatenation - getting wider

In a horizontal concatenation you combine all of the columns from a list of DataFrames into a single wider DataFrame.

let dfH1 = newDataFrame:
  l1: [1, 2]
  l2: [3, 4]
let dfH2 = newDataFrame:
  r1: [5, 6]
  r2: [7, 8]
  r3: [9, 10]
let dfHorizontalConcat = concat([dfH1, dfH2], how = Horizontal)
nbShow dfHorizontalConcat
shape: (2, 5)
l1l2r1r2r3
i64i64i64i64i64
13579
246810

Horizontal concatenation fails when dataframes have overlapping columns. Moreover, it is required that both dataframes have the same height. To combine dataframes with different height, use how="horizontal_extend", which pads shorter columns with null values at the end to match the height of the longest dataframe.

Diagonal concatenation - getting longer, wider and nullier

In a diagonal concatenation you combine all of the row and columns from a list of DataFrames into a single longer and/or wider DataFrame.

let dfD1 = newDataFrame:
  a: [1]
  b: [3]
let dfD2 = newDataFrame:
  a: [2]
  d: [4]
let dfDiagonalConcat = concat([dfD1, dfD2], how = Diagonal)
nbShow dfDiagonalConcat
shape: (2, 3)
abd
i64i64i64
13null
2null4

Diagonal concatenation generates nulls when the column names do not overlap.

When the dataframe shapes do not match and we have an overlapping semantic key then we can join the dataframes instead of concatenating them.

import nanuqbook
bookInit()

nbText: "# Concatenation"

attribution userGuide("Concatenation", "transformations/concatenation",
  "Left out: `how=\"horizontal_extend\"` and the section on rechunking, since Nanuq's " &
  "`concat` has neither yet.")

nbText: """
There are a number of ways to concatenate data from separate DataFrames:

- two dataframes with **the same columns** can be **vertically** concatenated to make a
  **longer** dataframe
- two dataframes with **non-overlapping columns** can be **horizontally** concatenated to
  make a **wider** dataframe
- two dataframes with **different numbers of rows and columns** can be **diagonally**
  concatenated to make a dataframe which might be longer and/ or wider. Where column names
  overlap values will be vertically concatenated. Where column names do not overlap new rows
  and columns will be added. Missing values will be set as `null`
"""

nanuq """
In Nanuq `concat` takes the frames as an array and the method as a value of the enum
`ConcatMethod`, where Python uses strings (`how="vertical"`).
"""

nbText: """
## Vertical concatenation - getting longer

In a vertical concatenation you combine all of the rows from a list of `DataFrames` into a
single longer `DataFrame`.
"""

tabs """
import polars as pl

df_v1 = pl.DataFrame({"a": [1], "b": [3]})
df_v2 = pl.DataFrame({"a": [2], "b": [4]})
pl.concat([df_v1, df_v2], how="vertical")
""":
  nbCode:
    import nanuq

    let dfV1 = newDataFrame:
      a: [1]
      b: [3]
    let dfV2 = newDataFrame:
      a: [2]
      b: [4]
    let dfVerticalConcat = concat([dfV1, dfV2], how = Vertical)
    nbShow dfVerticalConcat

nbText: """
Vertical concatenation fails when the dataframes do not have the same column names.

## Horizontal concatenation - getting wider

In a horizontal concatenation you combine all of the columns from a list of `DataFrames`
into a single wider `DataFrame`.
"""

tabs """
df_h1 = pl.DataFrame({"l1": [1, 2], "l2": [3, 4]})
df_h2 = pl.DataFrame({"r1": [5, 6], "r2": [7, 8], "r3": [9, 10]})
pl.concat([df_h1, df_h2], how="horizontal")
""":
  nbCode:
    let dfH1 = newDataFrame:
      l1: [1, 2]
      l2: [3, 4]
    let dfH2 = newDataFrame:
      r1: [5, 6]
      r2: [7, 8]
      r3: [9, 10]
    let dfHorizontalConcat = concat([dfH1, dfH2], how = Horizontal)
    nbShow dfHorizontalConcat

nbText: """
Horizontal concatenation fails when dataframes have overlapping columns. Moreover, it is
required that both dataframes have the same height. To combine dataframes with different
height, use `how="horizontal_extend"`, which pads shorter columns with `null` values at the
end to match the height of the longest dataframe.
"""

nanuq """
Nanuq does not have `how="horizontal_extend"` yet, so the original's example of it is left
out.
"""

nbText: """
## Diagonal concatenation - getting longer, wider and `null`ier

In a diagonal concatenation you combine all of the row and columns from a list of
`DataFrames` into a single longer and/or wider `DataFrame`.
"""

tabs """
df_d1 = pl.DataFrame({"a": [1], "b": [3]})
df_d2 = pl.DataFrame({"a": [2], "d": [4]})
pl.concat([df_d1, df_d2], how="diagonal")
""":
  nbCode:
    let dfD1 = newDataFrame:
      a: [1]
      b: [3]
    let dfD2 = newDataFrame:
      a: [2]
      d: [4]
    let dfDiagonalConcat = concat([dfD1, dfD2], how = Diagonal)
    nbShow dfDiagonalConcat

nbText: """
Diagonal concatenation generates nulls when the column names do not overlap.

When the dataframe shapes do not match and we have an overlapping semantic key then
[we can join the dataframes](joins.html) instead of concatenating them.
"""

nanuq """
The original ends with a section on rechunking. Nanuq's `concat` has no `rechunk` option
yet, so it is left out.
"""

bookSave()
Made with nimib and nimibook. Blocks marked "ran" were executed when this page was built.