Introduction to Polars
Liam Brannigan
Data Scientist & Polars Contributor
rentals.with_columns(
rentals.with_columns(pl.col("name","type").str.len_chars())
shape: (49, 8)
| name | type | price | bedrooms | doubles | singles | review | beach |
| --- | --- | --- | --- | --- | --- | --- | --- |
| u32 | u32 | i64 | i64 | i64 | i64 | f64 | bool |
|------|------|-------|----------|---------|---------|--------|-------|
| 5 | 7 | 540 | 4 | 1 | 2 | 8.9 | false |
| 9 | 7 | 540 | 4 | 2 | 2 | 8.7 | true |
| ... | ... | ... | ... | ... | ... | ... | ... |
rentals.with_columns(pl.col(pl.String).str.len_chars())
shape: (49, 8)
| name | type | price | bedrooms | doubles | singles | review | beach |
| --- | --- | --- | --- | --- | --- | --- | --- |
| u32 | u32 | i64 | i64 | i64 | i64 | f64 | bool |
|------|------|-------|----------|---------|---------|--------|-------|
| 5 | 7 | 540 | 4 | 1 | 2 | 8.9 | false |
| 9 | 7 | 540 | 4 | 2 | 2 | 8.7 | true |
| ... | ... | ... | ... | ... | ... | ... | ... |
pl.Int64
pl.Float64
pl.Boolean
rentals.with_columns(
pl.selectors.string().str.len_chars(),
)
shape: (49, 8)
| name | type | price | bedrooms | doubles | singles | review | beach |
| --- | --- | --- | --- | --- | --- | --- | --- |
| u32 | u32 | i64 | i64 | i64 | i64 | f64 | bool |
|------|------|-------|----------|---------|---------|--------|-------|
| 5 | 7 | 540 | 4 | 1 | 2 | 8.9 | false |
| 9 | 7 | 540 | 4 | 2 | 2 | 8.7 | true |
| ... | ... | ... | ... | ... | ... | ... | ... |
rentals.select(
pl.selectors.ends_with("s")
)
shape: (49, 3)
| bedrooms | doubles | singles |
| --- | --- | --- |
| i64 | i64 | i64 |
|----------|---------|---------|
| 4 | 1 | 2 |
| 4 | 2 | 2 |
| ... | ... | ... |
rentals.select(
pl.selectors.string() | pl.selectors.ends_with("s")
)
shape: (49, 5)
| name | type | bedrooms | doubles | singles |
| --- | --- | --- | --- | --- |
| str | str | i64 | i64 | i64 |
|---------------------|---------|----------|---------|---------|
| Waves | Cottage | 4 | 1 | 2 |
| Seashells | Cottage | 4 | 2 | 2 |
| Lake view | Cottage | 3 | 1 | 4 |
| ... | ... | ... | ... | ... |
pl.selectors.float()
pl.selectors.integers()
pl.selectors.string()
pl.selectors.starts_with("p")
pl.selectors.ends_with("e")
rentals.select(
pl.col("price","review").min()
)
shape: (1, 2)
| price | review |
| --- | --- |
| i64 | f64 |
|-------|--------|
| 249 | 7.2 |
rentals.select(
pl.col("price","review").min().name.suffix("_min"),
pl.col("price","review").max().name.suffix("_max")
)
shape: (1, 4)
| price_min | review_min | price_max | review_max |
| --- | --- | --- | --- |
| i64 | f64 | i64 | f64 |
|-----------|------------|-----------|------------|
| 249 | 7.2 | 2411 | 9.9 |
rentals.with_columns(pl.exclude("beach").cast(pl.String))
shape: (49, 8)
| name | type | price | bedrooms | doubles | singles | review | beach |
| --- | --- | --- | --- | --- | --- | --- | --- |
| str | str | str | str | str | str | str | bool |
|-----------|---------|-------|----------|---------|---------|--------|-------|
| Waves | Cottage | 540 | 4 | 1 | 2 | 8.9 | false |
| Seashells | Cottage | 540 | 4 | 2 | 2 | 8.7 | true |
| ... | ... | ... | ... | ... | ... | ... | ... |
Introduction to Polars