Pivot ve unpivot

Polars'a Giriş

Liam Brannigan

Data Scientist & Polars Contributor

Uzun vs. geniş biçim

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       |
Polars'a Giriş

Uzun vs. geniş biçim

rentals_long
shape: (4, 3)
| name      | bedroom_type | count |
| ---       | ---          | ---   |
| str       | str          | i64   |
|-----------|--------------|-------|
| Waves     | double       | 1     |
| Waves     | single       | 2     |
| Seashells | double       | 2     |
| Seashells | single       | 2     |
Polars'a Giriş

Uzundan genişe 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(



)
Polars'a Giriş

Uzundan genişe 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",


)
Polars'a Giriş

Uzundan genişe 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",
    index="name",

)
Polars'a Giriş

Uzundan genişe 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",
    index="name",
    values="count"
)
shape: (2, 3)
| name      | double | single |
| ---       | ---    | ---    |
| str       | i64    | i64    |
|-----------|--------|--------|
| Waves     | 1      | 2      |
| Seashells | 2      | 2      |
Polars'a Giriş

Toplamalı pivotlama

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     |
Polars'a Giriş

Toplamalı pivotlama

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   |
Polars'a Giriş

Genişten uzuna unpivot

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

Genişten uzuna unpivot

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


)
Polars'a Giriş

Genişten uzuna unpivot

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

)
Polars'a Giriş

Genişten uzuna unpivot

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     |
Polars'a Giriş

Genişten uzuna unpivot

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     |
| ...       | ...          |...    |
Polars'a Giriş

Hadi pratik yapalım!

Polars'a Giriş

Preparing Video For Download...