Pivoting and unpivoting

Introduction to Polars

Liam Brannigan

Data Scientist & Polars Contributor

Long vs. wide format

rentals_long
shape: (4, 3)
| name      | bedroom_type | count |
| ---       | ---          | ---   |
| str       | str          | i64   |
|-----------|--------------|-------|
| Waves     | double       | 1     |
| Waves     | single       | 2     |
| Seashells | double       | 2     |
| Seashells | single       | 2     |
rentals_wide
shape: (2, 3)
| name      | doubles | singles |
| ---       | ---     | ---     |
| str       | i64     | i64     |
|-----------|---------|---------|
| Waves     | 1       | 2       |
| Seashells | 2       | 2       |
Introduction to Polars

Long vs. wide format

rentals_long
shape: (4, 3)
| name      | bedroom_type | count |
| ---       | ---          | ---   |
| str       | str          | i64   |
|-----------|--------------|-------|
| Waves     | double       | 1     |
| Waves     | single       | 2     |
| Seashells | double       | 2     |
| Seashells | single       | 2     |
Introduction to Polars

Pivoting from long to wide

rentals_long
shape: (4, 3)
| name      | bedroom_type | count |
| ---       | ---          | ---   |
| str       | str          | i64   |
|-----------|--------------|-------|
| Waves     | double       | 1     |
| Waves     | single       | 2     |
| Seashells | double       | 2     |
| Seashells | single       | 2     |
rentals_long.pivot(



)
Introduction to Polars

Pivoting from long to wide

rentals_long
shape: (4, 3)
| name      | bedroom_type | count |
| ---       | ---          | ---   |
| str       | str          | i64   |
|-----------|--------------|-------|
| Waves     | double       | 1     |
| Waves     | single       | 2     |
| Seashells | double       | 2     |
| Seashells | single       | 2     |
rentals_long.pivot(
    on="bedroom_type",


)
Introduction to Polars

Pivoting from long to wide

rentals_long
shape: (4, 3)
| name      | bedroom_type | count |
| ---       | ---          | ---   |
| str       | str          | i64   |
|-----------|--------------|-------|
| Waves     | double       | 1     |
| Waves     | single       | 2     |
| Seashells | double       | 2     |
| Seashells | single       | 2     |
rentals_long.pivot(
    on="bedroom_type",
    index="name",

)
Introduction to Polars

Pivoting from long to wide

rentals_long
shape: (4, 3)
| name      | bedroom_type | count |
| ---       | ---          | ---   |
| str       | str          | i64   |
|-----------|--------------|-------|
| Waves     | double       | 1     |
| Waves     | single       | 2     |
| Seashells | double       | 2     |
| Seashells | single       | 2     |
rentals_long.pivot(
    on="bedroom_type",
    index="name",
    values="count"
)
shape: (2, 3)
| name      | double | single |
| ---       | ---    | ---    |
| str       | i64    | i64    |
|-----------|--------|--------|
| Waves     | 1      | 2      |
| Seashells | 2      | 2      |
Introduction to Polars

Pivoting with aggregations

rentals_multi
shape: (5, 3)
| name      | bedroom_type | count |
| ---       | ---          | ---   |
| str       | str          | i64   |
|-----------|--------------|-------|
| Waves     | double       | 1     |
| Waves     | single       | 1     |
| Waves     | single       | 1     |
| Seashells | double       | 1     |
| Seashells | double       | 1     |
Introduction to Polars

Pivoting with aggregations

rentals_multi.pivot(
    on="bedroom_type",
    index="name",
    values="count",
    aggregate_function="sum"
)
shape: (2, 3)
| name      | double | single |
| ---       | ---    | ---    |
| str       | i64    | i64    |
|-----------|--------|--------|
| Waves     | 1      | 2      |
| Seashells | 2      | null   |
Introduction to Polars

Unpivoting from wide to long

rentals_wide
shape: (2, 3)
| name      | doubles | singles |
| ---       | ---     | ---     |
| str       | i64     | i64     |
|-----------|---------|---------|
| Waves     | 1       | 2       |
| Seashells | 2       | 2       |
Introduction to Polars

Unpivoting from wide to long

rentals_wide
shape: (2, 3)
| name      | doubles | singles |
| ---       | ---     | ---     |
| str       | i64     | i64     |
|-----------|---------|---------|
| Waves     | 1       | 2       |
| Seashells | 2       | 2       |
  • Pandas: .melt()
rentals_wide.unpivot(


)
Introduction to Polars

Unpivoting from wide to long

rentals_wide
shape: (2, 3)
| name      | doubles | singles |
| ---       | ---     | ---     |
| str       | i64     | i64     |
|-----------|---------|---------|
| Waves     | 1       | 2       |
| Seashells | 2       | 2       |
rentals_wide.unpivot(
    index="name",

)
Introduction to Polars

Unpivoting from wide to long

rentals_wide
shape: (2, 3)
| name      | doubles | singles |
| ---       | ---     | ---     |
| str       | i64     | i64     |
|-----------|---------|---------|
| Waves     | 1       | 2       |
| Seashells | 2       | 2       |
rentals_wide.unpivot(
    index="name",
    on=["doubles", "singles"]
)
shape: (4, 3)
| name      | variable | value |
| ---       | ---      | ---   |
| str       | str      | i64   |
|-----------|----------|-------|
| Waves     | doubles  | 1     |
| Seashells | doubles  | 2     |
| Waves     | singles  | 2     |
| Seashells | singles  | 2     |
Introduction to Polars

Unpivoting from wide to long

rentals_wide.unpivot(
    index="name",
    on=["doubles", "singles"]
    variable_name="bedroom_type", value_name="count"
)
shape: (4, 3)
| name      | bedroom_type | count |
| ---       | ---          | ---   |
| str       | str          | i64   |
|-----------|--------------|-------|
| Waves     | doubles      | 1     |
| Seashells | doubles      | 2     |
| ...       | ...          |...    |
Introduction to Polars

Let's practice!

Introduction to Polars

Preparing Video For Download...