Introduction to Polars
Liam Brannigan
Data Scientist and Polars Contributor
rentals
shape: (49, 8)
| name | type | price | bedrooms | doubles | singles | review | beach |
| --- | --- | --- | --- | --- | --- | --- | --- |
| str | str | i64 | i64 | i64 | i64 | f64 | bool |
|----------|---------|-------|----------|---------|---------|--------|-------|
| waves | cottage | 540 | 4 | 1 | 2 | 8.9 | false |
| seashell | cottage | 540 | 4 | 2 | 2 | 8.7 | true |
| ... | ... | ... | ... | ... | ... | ... | ... |
rentals.with_columns(
)
rentals.with_columns(
(pl.col("doubles") * 2
)
rentals.with_columns(
(pl.col("doubles") * 2 + pl.col("singles"))
)
rentals.with_columns(
(pl.col("doubles") * 2 + pl.col("singles")).alias("total")
)
shape: (49, 9)
| name | type | ... | doubles | singles | review | beach | total |
| --- | --- | ... | --- | --- | --- | --- | --- |
| str | str | ... | i64 | i64 | f64 | bool | i64 |
|-----------|---------| ... |---------|---------|--------|-------|-------|
| Waves | Cottage | ... | 1 | 2 | 8.9 | false | 4 |
| Seashells | Cottage | ... | 2 | 2 | 8.7 | true | 6 |
| ... | ... | ... | ... | ... | ... | ... | ... |
rentals.with_columns(
pl.col("price").mean().alias("avg_price"),
)
shape: (49, 9)
| name | type | price | ... | review | beach | avg_price |
| --- | --- | --- | ... | --- | --- | --- |
| str | str | i64 | ... | f64 | bool | f64 |
|-----------|---------|-------| ... |--------|-------|-----------|
| Waves | Cottage | 540 | ... | 8.9 | false | 973.10204 |
| Seashells | Cottage | 540 | ... | 8.7 | true | 973.10204 |
| ... | ... | ... | ... | ... | ... | ... |
rentals
shape: (49, 8)
| name | type | price | bedrooms | doubles | singles | review | beach |
| --- | --- | --- | --- | --- | --- | --- | --- |
| str | str | i64 | i64 | i64 | i64 | f64 | bool |
|----------|---------|-------|----------|---------|---------|--------|-------|
| waves | cottage | 540 | 4 | 1 | 2 | 8.9 | false |
| seashell | cottage | 540 | 4 | 2 | 2 | 8.7 | true |
| ... | ... | ... | ... | ... | ... | ... | ... |
rentals.with_columns(
pl.col("bedrooms").cast(pl.Int16)
)
shape: (49, 8)
| name | type | price | bedrooms | doubles | singles | review | beach |
| --- | --- | --- | --- | --- | --- | --- | --- |
| str | str | i64 | i16 | i64 | i64 | f64 | bool |
|-----------|---------|-------|----------|---------|---------|--------|-------|
| Waves | Cottage | 540 | 4 | 1 | 2 | 8.9 | false |
| Seashells | Cottage | 540 | 4 | 2 | 2 | 8.7 | true |
| Lake view | Cottage | 714 | 3 | 1 | 4 | 9.2 | true |
rentals
shape: (49, 8)
| name | type | price | bedrooms | doubles | singles | review | beach |
| --- | --- | --- | --- | --- | --- | --- | --- |
| str | str | i64 | i64 | i64 | i64 | f64 | bool |
|----------|---------|-------|----------|---------|---------|--------|-------|
| waves | cottage | 540 | 4 | 1 | 2 | 8.9 | false |
| seashell | cottage | 540 | 4 | 2 | 2 | 8.7 | true |
| ... | ... | ... | ... | ... | ... | ... | ... |
rentals.rename({
"doubles": "double_beds",
"singles": "single_beds",
})
shape: (49, 8)
| name | type | price | ... | double_beds | single_beds | review | beach |
| --- | --- | --- | ... | --- | --- | --- | --- |
| str | str | i64 | ... | i64 | i64 | f64 | bool |
|-----------|---------|-------| ... |-------------|-------------|--------|-------|
| Waves | Cottage | 540 | ... | 1 | 2 | 8.9 | false |
| Seashells | Cottage | 540 | ... | 2 | 2 | 8.7 | true |
| ... | ... | ... | ... | ... | ... | ... | ... |
rentals.drop("review", "beach")
shape: (49, 6)
| name | type | price | bedrooms | doubles | singles |
| --- | --- | --- | --- | --- | --- |
| str | str | i64 | i64 | i64 | i64 |
|-----------|---------|-------|----------|---------|---------|
| Waves | Cottage | 540 | 4 | 1 | 2 |
| Seashells | Cottage | 540 | 4 | 2 | 2 |
| ... | ... | ... | ... | ... | ... |
Introduction to Polars