Introduction to Polars
Liam Brannigan
Data Scientist & Polars Contributor
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 |
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
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(
)
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",
)
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",
)
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 |
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 |
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 |
rentals_wide
shape: (2, 3)
| name | doubles | singles |
| --- | --- | --- |
| str | i64 | i64 |
|-----------|---------|---------|
| Waves | 1 | 2 |
| Seashells | 2 | 2 |
rentals_wide
shape: (2, 3)
| name | doubles | singles |
| --- | --- | --- |
| str | i64 | i64 |
|-----------|---------|---------|
| Waves | 1 | 2 |
| Seashells | 2 | 2 |
.melt()
rentals_wide.unpivot(
)
rentals_wide
shape: (2, 3)
| name | doubles | singles |
| --- | --- | --- |
| str | i64 | i64 |
|-----------|---------|---------|
| Waves | 1 | 2 |
| Seashells | 2 | 2 |
rentals_wide.unpivot(
index="name",
)
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 |
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