DataFrames verknüpfen

Daten­transformation mit Polars

Liam Brannigan

Data Scientist & Polars Contributor

Reviews-Daten

shape: (5, 4)
| business         | location    | review | price |
| ---              | ---         | ---    | ---   |
| str              | str         | f64    | i64   |
|------------------|-------------|--------|-------|
| 7burgers         | Wakey Wakey | 4.2    | 15    |
| Bang Bang Burger | Forest Rd.  | 3.8    | 12    |
| Costa Coffee     | City Point  | 4.5    | 8     |
| Costa Coffee     | Waterloo    | 4.1    | 8     |
| The Queens Head  | Denman St.  | 4.7    | 25    |
Daten­transformation mit Polars

Das Inspections-Datenset

inspections = pl.read_csv("restaurant_inspections.csv")
shape: (5, 4)
| business        | location    | type       | hygiene_rating |
| ---             | ---         | ---        | ---            |
| str             | str         | str        | i64            |
|-----------------|-------------|------------|----------------|
| 7burgers        | Wakey Wakey | restaurant | 4              |
| Costa Coffee    | City Point  | café       | 5              |
| Costa Coffee    | Waterloo    | café       | 3              |
| The Queens Head | Denman St.  | bar        | 5              |
| Wagamama        | Soho        | restaurant | 4              |
Daten­transformation mit Polars

Was ist ein Join?

Diagramm: Ein einzelner DataFrame.

Daten­transformation mit Polars

Was ist ein Join?

Diagramm: Zwei DataFrames.

Daten­transformation mit Polars

Was ist ein Join?

Diagramm: Zwei DataFrames mit markiertem passenden Schlüssel.

Daten­transformation mit Polars

Was ist ein Join?

Diagramm: Zwei DataFrames werden über gemeinsame Spalten zeilenweise zusammengeführt.

Daten­transformation mit Polars

Was ist ein Join?

Diagramm: Zwei DataFrames werden über gemeinsame Spalten zusammengeführt; ein passender Schlüssel ist markiert.

Daten­transformation mit Polars

Join über eine Spalte

reviews.join(                                       )
Daten­transformation mit Polars

Join über eine Spalte

reviews.join(inspections,                           )
Daten­transformation mit Polars

Join über eine Spalte

reviews.join(inspections, on="business"             )
Daten­transformation mit Polars

Join über eine Spalte

reviews.join(inspections, on="business", how="inner")
shape: (6, 7)
| business      | location    | ... | location_right | hygiene_rating |
| ---           | ---         | ... | ---            | ---            |
| str           | str         | ... | str            | i64            |
|---------------|-------------|-----|----------------|----------------|
| 7burgers      | Wakey Wakey | ... | Wakey Wakey    | 4              |
| Costa Coffee  | City Point  | ... | City Point     | 5              |
| Costa Coffee  | Waterloo    | ... | City Point     | 5              |
| Costa Coffee  | City Point  | ... | Waterloo       | 3              |
| Costa Coffee  | Waterloo    | ... | Waterloo       | 3              |
| The Queens Hd | Denman St.  | ... | Denman St.     | 5              |
Daten­transformation mit Polars

Join über mehrere Spalten

reviews.join(inspections, on=["business", "location"])
Daten­transformation mit Polars

Join über mehrere Spalten

reviews.join(inspections, on=["business", "location"])
shape: (4, 6)
| business        | location    | review | price | type       | hygiene_rating |
| ---             | ---         | ---    | ---   | ---        | ---            |
| str             | str         | f64    | i64   | str        | i64            |
|-----------------|-------------|--------|-------|------------|----------------|
| 7burgers        | Wakey Wakey | 4.2    | 15    | restaurant | 4              |
| Costa Coffee    | City Point  | 4.5    | 8     | café       | 5              |
| Costa Coffee    | Waterloo    | 4.1    | 8     | café       | 3              |
| The Queens Head | Denman St.  | 4.7    | 25    | bar        | 5              |
Daten­transformation mit Polars

Left Join

reviews.join(inspections, on=["business", "location"],           )
Daten­transformation mit Polars

Left Join

reviews.join(inspections, on=["business", "location"], how="left")
shape: (5, 6)
| business         | location    | review | price | type       | hygiene_rating |
| ---              | ---         | ---    | ---   | ---        | ---            |
| str              | str         | f64    | i64   | str        | i64            |
|------------------|-------------|--------|-------|------------|----------------|
| 7burgers         | Wakey Wakey | 4.2    | 15    | restaurant | 4              |
| Bang Bang Burger | Forest Rd.  | 3.8    | 12    | null       | null           |
| Costa Coffee     | City Point  | 4.5    | 8     | café       | 5              |
| Costa Coffee     | Waterloo    | 4.1    | 8     | café       | 3              |
| The Queens Head  | Denman St.  | 4.7    | 25    | bar        | 5              |
Daten­transformation mit Polars

Full Join

reviews.join(inspections, on=["business", "location"],                          )
Daten­transformation mit Polars

Full Join

reviews.join(inspections, on=["business", "location"], how="full"               )
Daten­transformation mit Polars

Full Join

reviews.join(inspections, on=["business", "location"], how="full", coalesce=True)
shape: (6, 6)
| business         | location    | review | price | type       | hygiene_rating |
| ---              | ---         | ---    | ---   | ---        | ---            |
| str              | str         | f64    | i64   | str        | i64            |
|------------------|-------------|--------|-------|------------|----------------|
| 7burgers         | Wakey Wakey | 4.2    | 15    | restaurant | 4              |
| Costa Coffee     | City Point  | 4.5    | 8     | café       | 5              |
| Costa Coffee     | Waterloo    | 4.1    | 8     | café       | 3              |
| The Queens Head  | Denman St.  | 4.7    | 25    | bar        | 5              |
| Wagamama         | Soho        | null   | null  | restaurant | 4              |
| Bang Bang Burger | Forest Rd.  | 3.8    | 12    | null       | null           |
Daten­transformation mit Polars

Join-Strategie wählen

2 Tabellen

Daten­transformation mit Polars

Inner Join

Darstellung: Inner Join

Daten­transformation mit Polars

Left Join

Darstellung: Left Join

Daten­transformation mit Polars

Full Join

Darstellung: Full Join

Daten­transformation mit Polars

Lass uns üben!

Daten­transformation mit Polars

Preparing Video For Download...