Data Transformation with Polars
Liam Brannigan
Data Scientist & Polars Contributor
shape: (5, 5)
| business | location | type | rating | capacity |
| --- | --- | --- | --- | --- |
| str | str | str | i64 | i64 |
|------------------|--------------|------------|--------|----------|
| 7burgers | Wakey Wakey | restaurant | 5 | 55 |
| Bang Bang Burger | Forest Rd. | restaurant | 4 | 55 |
| Costa Coffee | City Point | café | 5 | 41 |
| Costa Coffee | The Moorgate | takeaway | 5 | 0 |
| The Queens Head | Denman St. | bar | 5 | 187 |
"burger" in the business nameratings.with_columns(
pl.col("business")
)
ratings.with_columns(
pl.col("business").str.contains("Burger")
)
ratings.with_columns(
pl.col("business").str.contains("Burger").alias("is_burger")
)
shape: (5, 6)
| business | location | type | rating | capacity | is_burger |
| --- | --- | --- | --- | --- | --- |
| str | str | str | i64 | i64 | bool |
|------------------|-----------------|------------|--------|----------|-----------|
| 7burgers | Wakey Wakey | restaurant | 5 | 55 | false |
| Bang Bang Burger | Forest Rd. | restaurant | 4 | 55 | true |
| Costa Coffee | City Point | café | 5 | 41 | false |
| Costa Coffee | The Moorgate | takeaway | 5 | 0 | false |
| The Queens Head | Denman St. | bar | 5 | 187 | false |
ratings.with_columns(
pl.col("business").str.to_lowercase()
)
ratings.with_columns(
pl.col("business").str.to_lowercase().str.contains("burger").alias("is_burger")
)
shape: (5, 6)
| business | location | type | rating | capacity | is_burger |
| --- | --- | --- | --- | --- | --- |
| str | str | str | i64 | i64 | bool |
|------------------|-----------------|------------|--------|----------|-----------|
| 7burgers | Wakey Wakey | restaurant | 5 | 55 | true |
| Bang Bang Burger | Forest Rd. | restaurant | 4 | 55 | true |
| Costa Coffee | City Point | café | 5 | 41 | false |
| Costa Coffee | The Moorgate | takeaway | 5 | 0 | false |
| The Queens Head | Denman St. | bar | 5 | 187 | false |
ratings.filter(
)
ratings.filter(
pl.col("business").str.to_lowercase().str.contains("burger")
)
shape: (5, 6)
| business | location | type | rating | capacity |
| --- | --- | --- | --- | --- |
| str | str | str | i64 | i64 |
|-------------------|-----------------|------------|--------|----------|
| 7burgers | Wakey Wakey | restaurant | 5 | 55 |
| Bang Bang Burger | Forest Rd. | restaurant | 4 | 55 |
| Bronson's Burgers | Arch 112 | takeaway | 4 | 0 |
| Burger & Lobster | Bow Bells House | restaurant | 5 | 40 |
| Ted Burgers | Prince Of Wales | takeaway | 4 | 0 |
shape: (5, 5)
| business | location | type | rating | capacity |
| --- | --- | --- | --- | --- |
| str | str | str | i64 | i64 |
|------------------|--------------|------------|--------|----------|
| 7burgers | Wakey Wakey | restaurant | 5 | 55 |
| bang bang burger | Forest Rd. | restaurant | 4 | 55 |
| costa coffee | City Point | café | 5 | 41 |
| costa coffee | The Moorgate | takeaway | 5 | 0 |
| the queens head | Denman St. | bar | 5 | 187 |
"burger" or "coffee" from the business columnratings.with_columns(
)
ratings.with_columns(
pl.col("business")
)
ratings.with_columns(
pl.col("business").str.extract("(burger)")
)
ratings.with_columns(
pl.col("business").str.extract("(burger)").alias("food")
)
shape: (5, 6)
| business | location | type | rating | capacity | food |
| --- | --- | --- | --- | --- | --- |
| str | str | str | i64 | i64 | str |
|------------------|-----------------|------------|--------|----------|--------|
| 7burgers | Wakey Wakey | restaurant | 5 | 55 | burger |
| bang bang burger | Forest Rd. | restaurant | 4 | 55 | burger |
| costa coffee | City Point | café | 5 | 41 | null |
| costa coffee | The Moorgate | takeaway | 5 | 0 | null |
| the queens head | Denman St. | bar | 5 | 187 | null |
ratings.with_columns(
pl.col("business").str.extract("(burger|coffee)").alias("food")
)
shape: (5, 6)
| business | location | type | rating | capacity | burger_name |
| --- | --- | --- | --- | --- | --- |
| str | str | str | i64 | i64 | str |
|------------------|-----------------|------------|--------|----------|-------------|
| 7burgers | Wakey Wakey | restaurant | 5 | 55 | burger |
| bang bang burger | Forest Rd. | restaurant | 4 | 55 | burger |
| costa coffee | City Point | café | 5 | 41 | coffee |
| costa coffee | The Moorgate | takeaway | 5 | 0 | coffee |
| the queens head | Denman St. | bar | 5 | 187 | null |
shape: (5, 6)
| business | location | type | rating | capacity | food |
| --- | --- | --- | --- | --- | --- |
| str | str | str | i64 | i64 | str |
|------------------|-----------------|------------|--------|----------|---------|
| 7burgers | Wakey Wakey | restaurant | 5 | 55 | burger |
| bang bang burger | Forest Rd. | restaurant | 4 | 55 | burger |
| costa coffee | City Point | café | 5 | 41 | coffee |
| costa coffee | The Moorgate | takeaway | 5 | 0 | coffee |
| the queens head | Denman St. | bar | 5 | 187 | null |
"Rd." with "Road"ratings.with_columns(
pl.col("location")
)
ratings.with_columns(
pl.col("location").str.replace("Rd.", "Road")
)
shape: (5, 6)
| business | location | type | rating | capacity | food |
| --- | --- | --- | --- | --- | --- |
| str | str | str | i64 | i64 | str |
|------------------|-----------------|------------|--------|----------|---------|
| 7burgers | Wakey Wakey | restaurant | 5 | 55 | burger |
| bang bang burger | Forest Road | restaurant | 4 | 55 | burger |
| costa coffee | City Point | café | 5 | 41 | coffee |
| costa coffee | The Moorgate | takeaway | 5 | 0 | coffee |
| the queens head | Denman St. | bar | 5 | 187 | null |
ratings.with_columns(
pl.col("location").str.replace("Rd.", "Road")
)
ratings.with_columns(
pl.col("location").str.replace_many( )
)
ratings.with_columns(
pl.col("location").str.replace("Rd.", "Road")
)
ratings.with_columns(
pl.col("location").str.replace_many(["Rd.", "St."], )
)
ratings.with_columns(
pl.col("location").str.replace("Rd.", "Road")
)
ratings.with_columns(
pl.col("location").str.replace_many(["Rd.", "St."], ["Road", "Street"])
)
shape: (5, 6)
| business | location | type | rating | capacity | food |
| --- | --- | --- | --- | --- | --- |
| str | str | str | i64 | i64 | str |
|------------------|-----------------|------------|--------|----------|---------|
| 7burgers | Wakey Wakey | restaurant | 5 | 55 | burger |
| bang bang burger | Forest Road | restaurant | 4 | 55 | burger |
| costa coffee | City Point | café | 5 | 41 | coffee |
| costa coffee | The Moorgate | takeaway | 5 | 0 | coffee |
| the queens head | Denman Street | bar | 5 | 187 | null |
Data Transformation with Polars