Adding columns

Introduction to Polars

Liam Brannigan

Data Scientist and Polars Contributor

Adding a new column

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  |
| ...      | ...     | ...   | ...      | ...     | ...     | ...    | ...   |

Introduction to Polars

Adding a new column

rentals.with_columns(

)
Introduction to Polars

Adding a new column

rentals.with_columns(
    (pl.col("doubles") * 2
)
Introduction to Polars

Adding a new column

rentals.with_columns(
    (pl.col("doubles") * 2 + pl.col("singles"))
)
Introduction to Polars

Adding a new column

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

.with_columns() or .select()?

Schematic showing that the .with_columns method adds a column to a DataFrame.

Introduction to Polars

.with_columns() or .select()?

Schematic showing that the .with_columns method adds a column to a DataFrame whereas the .select method subsets the columns of a DataFrame.

Introduction to Polars

Adding an aggregated column

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

Changing the dtype of a column

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

Changing the dtype of a column

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

Renaming columns

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  |
| ...      | ...     | ...   | ...      | ...     | ...     | ...    | ...   |

Introduction to Polars

Renaming columns

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

Removing a column

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

Let's practice!

Introduction to Polars

Preparing Video For Download...