Reshaping

The same data can be laid out in more than one shape. Reshaping moves values between rows and columns without changing what they say.

Wide versus long dataframes

In a wide frame each subject is a column, one row per student:

import std/sugar
import nanuq

let gradesWide = newDataFrame:
  student: ["Jeroen", "Thijs", "Ritchie"]
  math: [85, 78, 92]
  science: [90, 82, 85]
  history: [88, 80, 87]
nbShow gradesWide
shape: (3, 4)
studentmathsciencehistory
stri64i64i64
"Jeroen"859088
"Thijs"788280
"Ritchie"928587

In a long frame each grade is a row, with the subject in a column of its own. Wide is easier to read; long is easier to filter, group and plot.

let gradesLong = newDataFrame:
  student: ["Jeroen", "Jeroen", "Jeroen", "Thijs", "Thijs", "Thijs",
            "Ritchie", "Ritchie", "Ritchie"]
  subject: ["Math", "Science", "History", "Math", "Science", "History",
            "Math", "Science", "History"]
  grade: [85, 90, 88, 78, 82, 80, 92, 85, 87]
nbShow gradesLong
shape: (9, 3)
studentsubjectgrade
strstri64
"Jeroen""Math"85
"Jeroen""Science"90
"Jeroen""History"88
"Thijs""Math"78
"Thijs""Science"82
"Thijs""History"80
"Ritchie""Math"92
"Ritchie""Science"85
"Ritchie""History"87

Pivot to a wider dataframe

pivot goes from long to wide: the distinct values of the on column become columns, one row per index value, and the cells come from values.

let grades = gradesLong
nbShow grades.pivot(index = "student", on = "subject", values = "grade")
shape: (3, 4)
studentMathScienceHistory
stri64i64i64
"Jeroen"859088
"Thijs"788280
"Ritchie"928587

When a student has more than one grade per subject, a cell gets several values, and they have to be combined:

let multipleGrades = newDataFrame:
  student: ["Jeroen", "Jeroen", "Jeroen", "Jeroen", "Jeroen", "Jeroen",
            "Thijs", "Thijs", "Thijs", "Thijs", "Thijs", "Thijs"]
  subject: ["Math", "Math", "Math", "Science", "Science", "Science",
            "Math", "Math", "Math", "Science", "Science", "Science"]
  grade: [85, 88, 85, 60, 66, 63, 51, 79, 62, 82, 85, 82]
nbShow multipleGrades
shape: (12, 3)
studentsubjectgrade
strstri64
"Jeroen""Math"85
"Jeroen""Math"88
"Jeroen""Math"85
"Jeroen""Science"60
"Jeroen""Science"66
………
"Thijs""Math"79
"Thijs""Math"62
"Thijs""Science"82
"Thijs""Science"85
"Thijs""Science"82
nbShow multipleGrades.pivot(index = "student", on = "subject", values = "grade",
                            aggregateFunction = PivotAgg.Mean)
shape: (2, 3)
studentMathScience
strf64f64
"Jeroen"86.063.0
"Thijs"64.083.0

Any expression works too: pl.element() stands for the values of one cell. Here, the spread between the best and the worst grade:

nbShow multipleGrades.pivot(index = "student", on = "subject", values = "grade",
                            aggregateFunction = element().max - element().min)
shape: (2, 3)
studentMathScience
stri64i64
"Jeroen"36
"Thijs"283

A pivot has to look at the data to know which columns it makes, so it is eager. On a LazyFrame, when you know the values of the on column in advance, you can write the pivot yourself as a group-by with one filtered aggregation per value:

let lf = newDataFrame(
  series("col1", ["a", "a", "a", "b", "b", "b"]),
  series("col2", ["x", "x", "x", "x", "y", "y"]),
  series("col3", [6, 7, 3, 2, 5, 7])).lazy

let index = col.col1
let on = col.col2
let values = col.col3
let uniqueColumnValues = ["x", "y"]
proc aggregateFunction(c: Expr): Expr = c.tanh.mean

let aggs = sugar.collect:
  for value in uniqueColumnValues:
    aggregateFunction(values.filter(on == value)).alias(value)
nbShow lf.groupBy(index).agg(aggs).collect
shape: (2, 3)
col1xy
strf64f64
"a"0.998347null
"b"0.9640280.999954

Unpivot to a longer dataframe

unpivot goes the other way: the on columns become two, one with the old column name and one with its value, and the index columns are repeated.

nbShow gradesWide.unpivot(
  index = ["student"],
  on = ["math", "science", "history"],
  variableName = "subject",
  valueName = "grade")
shape: (9, 3)
studentsubjectgrade
strstri64
"Jeroen""math"85
"Thijs""math"78
"Ritchie""math"92
"Jeroen""science"90
"Thijs""science"82
"Ritchie""science"85
"Jeroen""history"88
"Thijs""history"80
"Ritchie""history"87

With more identifying columns, all of them go in index:

let df = newDataFrame:
  student: ["Jeroen", "Thijs", "Ritchie", "Jeroen", "Thijs", "Ritchie"]
  class: ["Math101", "Math101", "Math101", "Math102", "Math102", "Math102"]
  age: [20, 21, 22, 20, 21, 22]
  semester: ["Fall", "Fall", "Fall", "Spring", "Spring", "Spring"]
  math: [85, 78, 92, 88, 79, 95]
  science: [90, 82, 85, 92, 81, 87]
  history: [88, 80, 87, 85, 82, 89]
nbShow df
shape: (6, 7)
studentclassagesemestermathsciencehistory
strstri64stri64i64i64
"Jeroen""Math101"20"Fall"859088
"Thijs""Math101"21"Fall"788280
"Ritchie""Math101"22"Fall"928587
"Jeroen""Math102"20"Spring"889285
"Thijs""Math102"21"Spring"798182
"Ritchie""Math102"22"Spring"958789
nbShow df.unpivot(
  index = ["student", "class", "age", "semester"],
  on = ["math", "science", "history"],
  variableName = "subject",
  valueName = "grade")
shape: (18, 6)
studentclassagesemestersubjectgrade
strstri64strstri64
"Jeroen""Math101"20"Fall""math"85
"Thijs""Math101"21"Fall""math"78
"Ritchie""Math101"22"Fall""math"92
"Jeroen""Math102"20"Spring""math"88
"Thijs""Math102"21"Spring""math"79
………………
"Thijs""Math101"21"Fall""history"80
"Ritchie""Math101"22"Fall""history"87
"Jeroen""Math102"20"Spring""history"85
"Thijs""Math102"21"Spring""history"82
"Ritchie""Math102"22"Spring""history"89

Transposing

transpose turns rows into columns and columns into rows, like a matrix. The old column names can be kept in a column of their own, and the new columns can be named:

let reportColumns = sugar.collect:
  for i in 0 ..< gradesWide.height: "report_" & $(i + 1)

nbShow gradesWide.transpose(
  includeHeader = true,
  headerName = "original_headers",
  columnNames = reportColumns)
shape: (4, 4)
original_headersreport_1report_2report_3
strstrstrstr
"student""Jeroen""Thijs""Ritchie"
"math""85""78""92"
"science""90""82""85"
"history""88""80""87"

Exploding

A list column can be turned into rows, one per value, with the other columns repeated:

var gradesNested = newDataFrame:
  student: ["Jeroen", "Thijs", "Ritchie"]
  math: [@[85, 90, 88], @[78, 82, 80], @[92, 85, 87]]

nbShow gradesNested
shape: (3, 2)
studentmath
strlist[i64]
"Jeroen"[85, 90, 88]
"Thijs"[78, 82, 80]
"Ritchie"[92, 85, 87]
nbShow gradesNested.explode("math")
shape: (9, 2)
studentmath
stri64
"Jeroen"85
"Jeroen"90
"Jeroen"88
"Thijs"78
"Thijs"82
"Thijs"80
"Ritchie"92
"Ritchie"85
"Ritchie"87

Several list columns explode together only when their lists have the same lengths, row by row:

gradesNested = newDataFrame:
  student: ["Jeroen", "Thijs", "Ritchie"]
  math: [@[85, 90, 88], @[78, 82, 80], @[92, 85, 87]]
  science: [@[85, 90, 88], @[78, 82], @[92, 85, 87]]
  history: [@[85, 90, 88], @[78, 82], @[92, 85, 87]]

nbShow gradesNested
shape: (3, 4)
studentmathsciencehistory
strlist[i64]list[i64]list[i64]
"Jeroen"[85, 90, 88][85, 90, 88][85, 90, 88]
"Thijs"[78, 82, 80][78, 82][78, 82]
"Ritchie"[92, 85, 87][92, 85, 87][92, 85, 87]
# This raises a ShapeError:
# gradesNested.explode("math", "science", "history")
doAssertRaises(ShapeError):
  discard gradesNested.explode("math", "science", "history")

Unpivoting first gives one list per row, which then explodes on its own:

let gradesNestedLong = gradesNested.unpivot(
  index = ["student"], variableName = "subject", valueName = "grade")

nbShow gradesNestedLong
shape: (9, 3)
studentsubjectgrade
strstrlist[i64]
"Jeroen""math"[85, 90, 88]
"Thijs""math"[78, 82, 80]
"Ritchie""math"[92, 85, 87]
"Jeroen""science"[85, 90, 88]
"Thijs""science"[78, 82]
"Ritchie""science"[92, 85, 87]
"Jeroen""history"[85, 90, 88]
"Thijs""history"[78, 82]
"Ritchie""history"[92, 85, 87]
nbShow gradesNestedLong.explode("grade")
shape: (25, 3)
studentsubjectgrade
strstri64
"Jeroen""math"85
"Jeroen""math"90
"Jeroen""math"88
"Thijs""math"78
"Thijs""math"82
………
"Thijs""history"78
"Thijs""history"82
"Ritchie""history"92
"Ritchie""history"85
"Ritchie""history"87

Lists of lists explode one level at a time:

let nestedLists = newDataFrame:
  id: [1, 2]
  nested_value: [@[@["a", "b"]], @[@["c"], @["d", "e"]]]
nbShow nestedLists
shape: (2, 2)
idnested_value
i64list[list[str]]
1[["a", "b"]]
2[["c"], ["d", "e"]]
nbShow nestedLists.explode("nested_value")
shape: (3, 2)
idnested_value
i64list[str]
1["a", "b"]
2["c"]
2["d", "e"]
nbShow nestedLists.explode("nested_value").explode("nested_value")
shape: (5, 2)
idnested_value
i64str
1"a"
1"b"
2"c"
2"d"
2"e"

Partition into multiple dataframes

partition_by splits a frame into one frame per value of a column:

let sales = newDataFrame:
  OrderID: [1, 2, 3, 4, 5, 6]
  Product: ["A", "B", "A", "C", "B", "A"]
  Quantity: [10, 5, 8, 7, 3, 12]
  Region: ["North", "South", "North", "West", "South", "West"]
for part in sales.partitionBy("Region"):
  echo part
shape: (2, 4)
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ OrderID ┆ Product ┆ Quantity ┆ Region β”‚
β”‚ ---     ┆ ---     ┆ ---      ┆ ---    β”‚
β”‚ i64     ┆ str     ┆ i64      ┆ str    β”‚
β•žβ•β•β•β•β•β•β•β•β•β•ͺ═════════β•ͺ══════════β•ͺ════════║
β”‚ 1       ┆ A       ┆ 10       ┆ North  β”‚
β”‚ 3       ┆ A       ┆ 8        ┆ North  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”˜
shape: (2, 4)
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ OrderID ┆ Product ┆ Quantity ┆ Region β”‚
β”‚ ---     ┆ ---     ┆ ---      ┆ ---    β”‚
β”‚ i64     ┆ str     ┆ i64      ┆ str    β”‚
β•žβ•β•β•β•β•β•β•β•β•β•ͺ═════════β•ͺ══════════β•ͺ════════║
β”‚ 2       ┆ B       ┆ 5        ┆ South  β”‚
β”‚ 5       ┆ B       ┆ 3        ┆ South  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”˜
shape: (2, 4)
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ OrderID ┆ Product ┆ Quantity ┆ Region β”‚
β”‚ ---     ┆ ---     ┆ ---      ┆ ---    β”‚
β”‚ i64     ┆ str     ┆ i64      ┆ str    β”‚
β•žβ•β•β•β•β•β•β•β•β•β•ͺ═════════β•ͺ══════════β•ͺ════════║
β”‚ 4       ┆ C       ┆ 7        ┆ West   β”‚
β”‚ 6       ┆ A       ┆ 12       ┆ West   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Without the key column:

for part in sales.partitionBy("Region", includeKey = false):
  echo part
shape: (2, 3)
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ OrderID ┆ Product ┆ Quantity β”‚
β”‚ ---     ┆ ---     ┆ ---      β”‚
β”‚ i64     ┆ str     ┆ i64      β”‚
β•žβ•β•β•β•β•β•β•β•β•β•ͺ═════════β•ͺ══════════║
β”‚ 1       ┆ A       ┆ 10       β”‚
β”‚ 3       ┆ A       ┆ 8        β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
shape: (2, 3)
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ OrderID ┆ Product ┆ Quantity β”‚
β”‚ ---     ┆ ---     ┆ ---      β”‚
β”‚ i64     ┆ str     ┆ i64      β”‚
β•žβ•β•β•β•β•β•β•β•β•β•ͺ═════════β•ͺ══════════║
β”‚ 2       ┆ B       ┆ 5        β”‚
β”‚ 5       ┆ B       ┆ 3        β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
shape: (2, 3)
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ OrderID ┆ Product ┆ Quantity β”‚
β”‚ ---     ┆ ---     ┆ ---      β”‚
β”‚ i64     ┆ str     ┆ i64      β”‚
β•žβ•β•β•β•β•β•β•β•β•β•ͺ═════════β•ͺ══════════║
β”‚ 4       ┆ C       ┆ 7        β”‚
β”‚ 6       ┆ A       ┆ 12       β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
import nanuqbook
bookInit()

nbText: "# Reshaping"

attribution definitiveGuide(15, "Reshaping",
  "Left out: " &
  "`partition_by(as_dict=True)`. The frames of `partition_by` are printed one by one " &
  "on both sides.")

nbText: """
The same data can be laid out in more than one shape. Reshaping moves values between rows
and columns without changing what they say.

## Wide versus long dataframes

In a *wide* frame each subject is a column, one row per student:
"""

tabs """
import polars as pl

grades_wide = pl.DataFrame(
    {
        "student": ["Jeroen", "Thijs", "Ritchie"],
        "math": [85, 78, 92],
        "science": [90, 82, 85],
        "history": [88, 80, 87],
    }
)

grades_wide
""":
  nbCode:
    import std/sugar
    import nanuq

    let gradesWide = newDataFrame:
      student: ["Jeroen", "Thijs", "Ritchie"]
      math: [85, 78, 92]
      science: [90, 82, 85]
      history: [88, 80, 87]
    nbShow gradesWide

nbText: """
In a *long* frame each grade is a row, with the subject in a column of its own. Wide is
easier to read; long is easier to filter, group and plot.
"""

tabs """
grades_long = pl.DataFrame(
    {
        "student": [
            "Jeroen",
            "Jeroen",
            "Jeroen",
            "Thijs",
            "Thijs",
            "Thijs",
            "Ritchie",
            "Ritchie",
            "Ritchie",
        ],
        "subject": [
            "Math",
            "Science",
            "History",
            "Math",
            "Science",
            "History",
            "Math",
            "Science",
            "History",
        ],
        "grade": [85, 90, 88, 78, 82, 80, 92, 85, 87],
    }
)

grades_long
""":
  nbCode:
    let gradesLong = newDataFrame:
      student: ["Jeroen", "Jeroen", "Jeroen", "Thijs", "Thijs", "Thijs",
                "Ritchie", "Ritchie", "Ritchie"]
      subject: ["Math", "Science", "History", "Math", "Science", "History",
                "Math", "Science", "History"]
      grade: [85, 90, 88, 78, 82, 80, 92, 85, 87]
    nbShow gradesLong

nbText: """
## Pivot to a wider dataframe

`pivot` goes from long to wide: the distinct values of the `on` column become columns, one
row per `index` value, and the cells come from `values`.
"""

tabs """
grades = pl.DataFrame(
    {
        "student": [
            "Jeroen",
            "Jeroen",
            "Jeroen",
            "Thijs",
            "Thijs",
            "Thijs",
            "Ritchie",
            "Ritchie",
            "Ritchie",
        ],
        "subject": [
            "Math",
            "Science",
            "History",
            "Math",
            "Science",
            "History",
            "Math",
            "Science",
            "History",
        ],
        "grade": [85, 90, 88, 78, 82, 80, 92, 85, 87],
    }
)

grades.pivot(index="student", on="subject", values="grade")
""":
  nbCode:
    let grades = gradesLong
    nbShow grades.pivot(index = "student", on = "subject", values = "grade")

nbText: """
When a student has more than one grade per subject, a cell gets several values, and they
have to be combined:
"""

tabs """
multiple_grades = pl.DataFrame(
    {
        "student": [
            "Jeroen",
            "Jeroen",
            "Jeroen",
            "Jeroen",
            "Jeroen",
            "Jeroen",
            "Thijs",
            "Thijs",
            "Thijs",
            "Thijs",
            "Thijs",
            "Thijs",
        ],
        "subject": [
            "Math",
            "Math",
            "Math",
            "Science",
            "Science",
            "Science",
            "Math",
            "Math",
            "Math",
            "Science",
            "Science",
            "Science",
        ],
        "grade": [85, 88, 85, 60, 66, 63, 51, 79, 62, 82, 85, 82],
    }
)

multiple_grades
""":
  nbCode:
    let multipleGrades = newDataFrame:
      student: ["Jeroen", "Jeroen", "Jeroen", "Jeroen", "Jeroen", "Jeroen",
                "Thijs", "Thijs", "Thijs", "Thijs", "Thijs", "Thijs"]
      subject: ["Math", "Math", "Math", "Science", "Science", "Science",
                "Math", "Math", "Math", "Science", "Science", "Science"]
      grade: [85, 88, 85, 60, 66, 63, 51, 79, 62, 82, 85, 82]
    nbShow multipleGrades

nanuq """
Python names the aggregation with a string, Nanuq with the enum `PivotAgg` (`First`,
`Last`, `Sum`, `Min`, `Max`, `Mean`, `Median`, `Len`, and `Item`, the default, which
fails when a cell gets more than one value).
"""

tabs """
multiple_grades.pivot(
    index="student", on="subject", values="grade", aggregate_function="mean"
)
""":
  nbCode:
    nbShow multipleGrades.pivot(index = "student", on = "subject", values = "grade",
                                aggregateFunction = PivotAgg.Mean)

nbText: """
Any expression works too: `pl.element()` stands for the values of one cell. Here, the
spread between the best and the worst grade:
"""

tabs """
multiple_grades.pivot(
    index="student",
    on="subject",
    values="grade",
    aggregate_function=pl.element().max() - pl.element().min(),
)
""":
  nbCode:
    nbShow multipleGrades.pivot(index = "student", on = "subject", values = "grade",
                                aggregateFunction = element().max - element().min)

nbText: """
A pivot has to look at the data to know which columns it makes, so it is eager. On a
LazyFrame, when you know the values of the `on` column in advance, you can write the pivot
yourself as a group-by with one filtered aggregation per value:
"""

nanuq """
Python builds the aggregations with a generator; Nim with `collect` from std/sugar
(written `sugar.collect`, since a LazyFrame has a `collect` too). The
aggregation function is a Nim proc. Nanuq's lazy `pivot` also takes the expected values,
as a frame: `lf.pivot(on = "col2", onColumns = newDataFrame(series("col2", ["x", "y"])),
index = "col1", values = "col3", aggregateFunction = element().tanh.mean)`.
"""

tabs """
lf = pl.LazyFrame(
    {
        "col1": ["a", "a", "a", "b", "b", "b"],
        "col2": ["x", "x", "x", "x", "y", "y"],
        "col3": [6, 7, 3, 2, 5, 7],
    }
)

index = pl.col("col1")
on = pl.col("col2")
values = pl.col("col3")
unique_column_values = ["x", "y"]
aggregate_function = lambda col: col.tanh().mean()

lf.group_by(index).agg(
    aggregate_function(values.filter(on == value)).alias(value)
    for value in unique_column_values
).collect()
""":
  nbCode:
    let lf = newDataFrame(
      series("col1", ["a", "a", "a", "b", "b", "b"]),
      series("col2", ["x", "x", "x", "x", "y", "y"]),
      series("col3", [6, 7, 3, 2, 5, 7])).lazy

    let index = col.col1
    let on = col.col2
    let values = col.col3
    let uniqueColumnValues = ["x", "y"]
    proc aggregateFunction(c: Expr): Expr = c.tanh.mean

    let aggs = sugar.collect:
      for value in uniqueColumnValues:
        aggregateFunction(values.filter(on == value)).alias(value)
    nbShow lf.groupBy(index).agg(aggs).collect

nbText: """
## Unpivot to a longer dataframe

`unpivot` goes the other way: the `on` columns become two, one with the old column name
and one with its value, and the `index` columns are repeated.
"""

tabs """
grades_wide.unpivot(
    index=["student"],
    on=["math", "science", "history"],
    variable_name="subject",
    value_name="grade",
)
""":
  nbCode:
    nbShow gradesWide.unpivot(
      index = ["student"],
      on = ["math", "science", "history"],
      variableName = "subject",
      valueName = "grade")

nbText: "With more identifying columns, all of them go in `index`:"

tabs """
df = pl.DataFrame(
    {
        "student": ["Jeroen", "Thijs", "Ritchie", "Jeroen", "Thijs", "Ritchie"],
        "class": [
            "Math101",
            "Math101",
            "Math101",
            "Math102",
            "Math102",
            "Math102",
        ],
        "age": [20, 21, 22, 20, 21, 22],
        "semester": ["Fall", "Fall", "Fall", "Spring", "Spring", "Spring"],
        "math": [85, 78, 92, 88, 79, 95],
        "science": [90, 82, 85, 92, 81, 87],
        "history": [88, 80, 87, 85, 82, 89],
    }
)
df
""":
  nbCode:
    let df = newDataFrame:
      student: ["Jeroen", "Thijs", "Ritchie", "Jeroen", "Thijs", "Ritchie"]
      class: ["Math101", "Math101", "Math101", "Math102", "Math102", "Math102"]
      age: [20, 21, 22, 20, 21, 22]
      semester: ["Fall", "Fall", "Fall", "Spring", "Spring", "Spring"]
      math: [85, 78, 92, 88, 79, 95]
      science: [90, 82, 85, 92, 81, 87]
      history: [88, 80, 87, 85, 82, 89]
    nbShow df

tabs """
df.unpivot(
    index=["student", "class", "age", "semester"],
    on=["math", "science", "history"],
    variable_name="subject",
    value_name="grade",
)
""":
  nbCode:
    nbShow df.unpivot(
      index = ["student", "class", "age", "semester"],
      on = ["math", "science", "history"],
      variableName = "subject",
      valueName = "grade")

nbText: """
## Transposing

`transpose` turns rows into columns and columns into rows, like a matrix. The old column
names can be kept in a column of their own, and the new columns can be named:
"""

nanuq """
Python passes the new names as a generator, made from the four column names: Polars
takes only the three it needs, one per row. Nanuq takes a seq with exactly one name per
row.
"""

tabs """
report_columns = (f"report_{i + 1}" for i, _ in enumerate(grades_wide.columns))

grades_wide.transpose(
    include_header=True,
    header_name="original_headers",
    column_names=report_columns,
)
""":
  nbCode:
    let reportColumns = sugar.collect:
      for i in 0 ..< gradesWide.height: "report_" & $(i + 1)

    nbShow gradesWide.transpose(
      includeHeader = true,
      headerName = "original_headers",
      columnNames = reportColumns)

nbText: """
## Exploding

A list column can be turned into rows, one per value, with the other columns repeated:
"""

tabs """
grades_nested = pl.DataFrame(
    {
        "student": ["Jeroen", "Thijs", "Ritchie"],
        "math": [[85, 90, 88], [78, 82, 80], [92, 85, 87]],
    }
)

grades_nested
""":
  nbCode:
    var gradesNested = newDataFrame:
      student: ["Jeroen", "Thijs", "Ritchie"]
      math: [@[85, 90, 88], @[78, 82, 80], @[92, 85, 87]]

    nbShow gradesNested

tabs """
grades_nested.explode("math")
""":
  nbCode:
    nbShow gradesNested.explode("math")

nbText: "Several list columns explode together only when their lists have the same lengths, row by row:"

tabs """
grades_nested = pl.DataFrame(
    {
        "student": ["Jeroen", "Thijs", "Ritchie"],
        "math": [[85, 90, 88], [78, 82, 80], [92, 85, 87]],
        "science": [[85, 90, 88], [78, 82], [92, 85, 87]],
        "history": [[85, 90, 88], [78, 82], [92, 85, 87]],
    }
)

grades_nested
""":
  nbCode:
    gradesNested = newDataFrame:
      student: ["Jeroen", "Thijs", "Ritchie"]
      math: [@[85, 90, 88], @[78, 82, 80], @[92, 85, 87]]
      science: [@[85, 90, 88], @[78, 82], @[92, 85, 87]]
      history: [@[85, 90, 88], @[78, 82], @[92, 85, 87]]

    nbShow gradesNested

tabs """
# This raises a ShapeError:
# grades_nested.explode("math", "science", "history")
""":
  nbCode:
    # This raises a ShapeError:
    # gradesNested.explode("math", "science", "history")
    doAssertRaises(ShapeError):
      discard gradesNested.explode("math", "science", "history")

nbText: "Unpivoting first gives one list per row, which then explodes on its own:"

tabs """
grades_nested_long = grades_nested.unpivot(
    index="student", variable_name="subject", value_name="grade"
)

grades_nested_long
""":
  nbCode:
    let gradesNestedLong = gradesNested.unpivot(
      index = ["student"], variableName = "subject", valueName = "grade")

    nbShow gradesNestedLong

tabs """
grades_nested_long.explode("grade")
""":
  nbCode:
    nbShow gradesNestedLong.explode("grade")

nbText: "Lists of lists explode one level at a time:"

tabs """
nested_lists = pl.DataFrame(
    {
        "id": [1, 2],
        "nested_value": [[["a", "b"]], [["c"], ["d", "e"]]],
    },
    strict=False,
)
nested_lists
""":
  nbCode:
    let nestedLists = newDataFrame:
      id: [1, 2]
      nested_value: [@[@["a", "b"]], @[@["c"], @["d", "e"]]]
    nbShow nestedLists

tabs """
nested_lists.explode("nested_value")
""":
  nbCode:
    nbShow nestedLists.explode("nested_value")

tabs """
nested_lists.explode("nested_value").explode("nested_value")
""":
  nbCode:
    nbShow nestedLists.explode("nested_value").explode("nested_value")

nbText: """
## Partition into multiple dataframes

`partition_by` splits a frame into one frame per value of a column:
"""

tabs """
sales = pl.DataFrame(
    {
        "OrderID": [1, 2, 3, 4, 5, 6],
        "Product": ["A", "B", "A", "C", "B", "A"],
        "Quantity": [10, 5, 8, 7, 3, 12],
        "Region": ["North", "South", "North", "West", "South", "West"],
    }
)
""":
  nbCode:
    let sales = newDataFrame:
      OrderID: [1, 2, 3, 4, 5, 6]
      Product: ["A", "B", "A", "C", "B", "A"]
      Quantity: [10, 5, 8, 7, 3, 12]
      Region: ["North", "South", "North", "West", "South", "West"]

nanuq """
`partitionBy` returns a `seq[DataFrame]` where Python returns a list; both sides print
the frames one by one.
"""

tabs """
for part in sales.partition_by("Region"):
    print(part)
""":
  nbCode:
    for part in sales.partitionBy("Region"):
      echo part

nbText: "Without the key column:"

tabs """
for part in sales.partition_by("Region", include_key=False):
    print(part)
""":
  nbCode:
    for part in sales.partitionBy("Region", includeKey = false):
      echo part

nanuq """
The book ends the section with `as_dict=True`, which returns a dictionary from each key
(a tuple) to its frame. Nanuq's `partitionBy` has no such option; in Nim, pair the frames
with their keys yourself, for example in a `Table`.
"""

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