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
| student | math | science | history |
|---|---|---|---|
| str | i64 | i64 | i64 |
| "Jeroen" | 85 | 90 | 88 |
| "Thijs" | 78 | 82 | 80 |
| "Ritchie" | 92 | 85 | 87 |
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| student | math | science | history |
|---|---|---|---|
| str | i64 | i64 | i64 |
| "Jeroen" | 85 | 90 | 88 |
| "Thijs" | 78 | 82 | 80 |
| "Ritchie" | 92 | 85 | 87 |
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
| student | subject | grade |
|---|---|---|
| str | str | i64 |
| "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 |
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| student | subject | grade |
|---|---|---|
| str | str | i64 |
| "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")
| student | Math | Science | History |
|---|---|---|---|
| str | i64 | i64 | i64 |
| "Jeroen" | 85 | 90 | 88 |
| "Thijs" | 78 | 82 | 80 |
| "Ritchie" | 92 | 85 | 87 |
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")| student | Math | Science | History |
|---|---|---|---|
| str | i64 | i64 | i64 |
| "Jeroen" | 85 | 90 | 88 |
| "Thijs" | 78 | 82 | 80 |
| "Ritchie" | 92 | 85 | 87 |
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
| student | subject | grade |
|---|---|---|
| str | str | i64 |
| "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 |
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| student | subject | grade |
|---|---|---|
| str | str | i64 |
| "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)
| student | Math | Science |
|---|---|---|
| str | f64 | f64 |
| "Jeroen" | 86.0 | 63.0 |
| "Thijs" | 64.0 | 83.0 |
multiple_grades.pivot(
index="student", on="subject", values="grade", aggregate_function="mean"
)| student | Math | Science |
|---|---|---|
| str | f64 | f64 |
| "Jeroen" | 86.0 | 63.0 |
| "Thijs" | 64.0 | 83.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)
| student | Math | Science |
|---|---|---|
| str | i64 | i64 |
| "Jeroen" | 3 | 6 |
| "Thijs" | 28 | 3 |
multiple_grades.pivot(
index="student",
on="subject",
values="grade",
aggregate_function=pl.element().max() - pl.element().min(),
)| student | Math | Science |
|---|---|---|
| str | i64 | i64 |
| "Jeroen" | 3 | 6 |
| "Thijs" | 28 | 3 |
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
| col1 | x | y |
|---|---|---|
| str | f64 | f64 |
| "a" | 0.998347 | null |
| "b" | 0.964028 | 0.999954 |
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()| col1 | x | y |
|---|---|---|
| str | f64 | f64 |
| "a" | 0.998347 | null |
| "b" | 0.964028 | 0.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")
| student | subject | grade |
|---|---|---|
| str | str | i64 |
| "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 |
grades_wide.unpivot(
index=["student"],
on=["math", "science", "history"],
variable_name="subject",
value_name="grade",
)| student | subject | grade |
|---|---|---|
| str | str | i64 |
| "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
| student | class | age | semester | math | science | history |
|---|---|---|---|---|---|---|
| str | str | i64 | str | i64 | i64 | i64 |
| "Jeroen" | "Math101" | 20 | "Fall" | 85 | 90 | 88 |
| "Thijs" | "Math101" | 21 | "Fall" | 78 | 82 | 80 |
| "Ritchie" | "Math101" | 22 | "Fall" | 92 | 85 | 87 |
| "Jeroen" | "Math102" | 20 | "Spring" | 88 | 92 | 85 |
| "Thijs" | "Math102" | 21 | "Spring" | 79 | 81 | 82 |
| "Ritchie" | "Math102" | 22 | "Spring" | 95 | 87 | 89 |
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| student | class | age | semester | math | science | history |
|---|---|---|---|---|---|---|
| str | str | i64 | str | i64 | i64 | i64 |
| "Jeroen" | "Math101" | 20 | "Fall" | 85 | 90 | 88 |
| "Thijs" | "Math101" | 21 | "Fall" | 78 | 82 | 80 |
| "Ritchie" | "Math101" | 22 | "Fall" | 92 | 85 | 87 |
| "Jeroen" | "Math102" | 20 | "Spring" | 88 | 92 | 85 |
| "Thijs" | "Math102" | 21 | "Spring" | 79 | 81 | 82 |
| "Ritchie" | "Math102" | 22 | "Spring" | 95 | 87 | 89 |
nbShow df.unpivot(
index = ["student", "class", "age", "semester"],
on = ["math", "science", "history"],
variableName = "subject",
valueName = "grade")
| student | class | age | semester | subject | grade |
|---|---|---|---|---|---|
| str | str | i64 | str | str | i64 |
| "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 |
df.unpivot(
index=["student", "class", "age", "semester"],
on=["math", "science", "history"],
variable_name="subject",
value_name="grade",
)| student | class | age | semester | subject | grade |
|---|---|---|---|---|---|
| str | str | i64 | str | str | i64 |
| "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)
| original_headers | report_1 | report_2 | report_3 |
|---|---|---|---|
| str | str | str | str |
| "student" | "Jeroen" | "Thijs" | "Ritchie" |
| "math" | "85" | "78" | "92" |
| "science" | "90" | "82" | "85" |
| "history" | "88" | "80" | "87" |
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,
)| original_headers | report_1 | report_2 | report_3 |
|---|---|---|---|
| str | str | str | str |
| "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
| student | math |
|---|---|
| str | list[i64] |
| "Jeroen" | [85, 90, 88] |
| "Thijs" | [78, 82, 80] |
| "Ritchie" | [92, 85, 87] |
grades_nested = pl.DataFrame(
{
"student": ["Jeroen", "Thijs", "Ritchie"],
"math": [[85, 90, 88], [78, 82, 80], [92, 85, 87]],
}
)
grades_nested| student | math |
|---|---|
| str | list[i64] |
| "Jeroen" | [85, 90, 88] |
| "Thijs" | [78, 82, 80] |
| "Ritchie" | [92, 85, 87] |
nbShow gradesNested.explode("math")
| student | math |
|---|---|
| str | i64 |
| "Jeroen" | 85 |
| "Jeroen" | 90 |
| "Jeroen" | 88 |
| "Thijs" | 78 |
| "Thijs" | 82 |
| "Thijs" | 80 |
| "Ritchie" | 92 |
| "Ritchie" | 85 |
| "Ritchie" | 87 |
grades_nested.explode("math")| student | math |
|---|---|
| str | i64 |
| "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
| student | math | science | history |
|---|---|---|---|
| str | list[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] |
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| student | math | science | history |
|---|---|---|---|
| str | list[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")# This raises a ShapeError:
# grades_nested.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
| student | subject | grade |
|---|---|---|
| str | str | list[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] |
grades_nested_long = grades_nested.unpivot(
index="student", variable_name="subject", value_name="grade"
)
grades_nested_long| student | subject | grade |
|---|---|---|
| str | str | list[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")
| student | subject | grade |
|---|---|---|
| str | str | i64 |
| "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 |
grades_nested_long.explode("grade")| student | subject | grade |
|---|---|---|
| str | str | i64 |
| "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
| id | nested_value |
|---|---|
| i64 | list[list[str]] |
| 1 | [["a", "b"]] |
| 2 | [["c"], ["d", "e"]] |
nested_lists = pl.DataFrame(
{
"id": [1, 2],
"nested_value": [[["a", "b"]], [["c"], ["d", "e"]]],
},
strict=False,
)
nested_lists| id | nested_value |
|---|---|
| i64 | list[list[str]] |
| 1 | [["a", "b"]] |
| 2 | [["c"], ["d", "e"]] |
nbShow nestedLists.explode("nested_value")
| id | nested_value |
|---|---|
| i64 | list[str] |
| 1 | ["a", "b"] |
| 2 | ["c"] |
| 2 | ["d", "e"] |
nested_lists.explode("nested_value")| id | nested_value |
|---|---|
| i64 | list[str] |
| 1 | ["a", "b"] |
| 2 | ["c"] |
| 2 | ["d", "e"] |
nbShow nestedLists.explode("nested_value").explode("nested_value")
| id | nested_value |
|---|---|
| i64 | str |
| 1 | "a" |
| 1 | "b" |
| 2 | "c" |
| 2 | "d" |
| 2 | "e" |
nested_lists.explode("nested_value").explode("nested_value")| id | nested_value |
|---|---|
| i64 | str |
| 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"]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"],
}
)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 β βββββββββββ΄ββββββββββ΄βββββββββββ΄βββββββββ
for part in sales.partition_by("Region"):
print(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 β βββββββββββ΄ββββββββββ΄βββββββββββ
for part in sales.partition_by("Region", include_key=False):
print(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()
On GitHub: book/definitive-guide/reshaping.nim