Working with multiple columns

Introduction to Polars

Liam Brannigan

Data Scientist & Polars Contributor

Using pl.col()

rentals.with_columns(
Introduction to Polars

Using pl.col()

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  |
| ...  | ...  | ...   | ...      | ...     | ...     | ...    | ...   |
Introduction to Polars

Using pl.col() with dtypes

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  |
| ...  | ...  | ...   | ...      | ...     | ...     | ...    | ...   |
Introduction to Polars

Using pl.col() with dtypes

  • pl.Int64
  • pl.Float64
  • pl.Boolean
1 Full set of dtypes: https://docs.pola.rs/api/python/stable/reference/datatypes.html
Introduction to Polars

Introducing selectors

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  |
| ...  | ...  | ...   | ...      | ...     | ...     | ...    | ...   |
Introduction to Polars

Name matching with selectors

rentals.select(
  pl.selectors.ends_with("s")
)
shape: (49, 3)
| bedrooms | doubles | singles |
| ---      | ---     | ---     |
| i64      | i64     | i64     |
|----------|---------|---------|
| 4        | 1       | 2       |
| 4        | 2       | 2       |
| ...      | ...     | ...     |
Introduction to Polars

Combining selectors

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       |
| ...                 | ...     | ...      | ...     | ...     |
Introduction to Polars

Selectors overview

  • pl.selectors.float()
  • pl.selectors.integers()
  • pl.selectors.string()
  • ...
  • pl.selectors.starts_with("p")
  • pl.selectors.ends_with("e")
1 https://docs.pola.rs/api/python/stable/reference/selectors.html
Introduction to Polars

Adding a suffix to a column name

rentals.select(
  pl.col("price","review").min()
)
shape: (1, 2)
| price | review |
| ---   | ---    |
| i64   | f64    |
|-------|--------|
| 249   | 7.2    |
Introduction to Polars

Adding a suffix to a column name

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        |
Introduction to Polars

Excluding a column

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

Let's practice!

Introduction to Polars

Preparing Video For Download...