Pivoting and unpivoting

Introduzione a Polars

Liam Brannigan

Data Scientist & Polars Contributor

Formato long vs. 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_wide
shape: (2, 3)
| name      | doubles | singles |
| ---       | ---     | ---     |
| str       | i64     | i64     |
|-----------|---------|---------|
| Waves     | 1       | 2       |
| Seashells | 2       | 2       |
Introduzione a Polars

Formato long vs. 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     |
Introduzione a Polars

Pivot da long a 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(



)
Introduzione a Polars

Pivot da long a 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",


)
Introduzione a Polars

Pivot da long a 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",

)
Introduzione a Polars

Pivot da long a 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      |
Introduzione a Polars

Pivot con aggregazioni

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     |
Introduzione a Polars

Pivot con aggregazioni

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   |
Introduzione a Polars

Unpivot da wide a long

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

Unpivot da wide a long

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


)
Introduzione a Polars

Unpivot da wide a long

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

)
Introduzione a Polars

Unpivot da wide a 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     |
Introduzione a Polars

Unpivot da wide a 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     |
| ...       | ...          |...    |
Introduzione a Polars

Passons à la pratique !

Introduzione a Polars

Preparing Video For Download...