typestar

Joining DataFrames in Python

Combining two frames on a shared key.

import polars as pl

people = pl.DataFrame({
    "id": [1, 2, 3],
    "name": ["ada", "grace", "katherine"],
})

scores = pl.DataFrame({
    "id": [1, 2, 4],
    "score": [95, 88, 70],
})

inner = people.join(scores, on="id")
left = people.join(scores, on="id", how="left")
outer = people.join(scores, on="id", how="full")
anti = people.join(scores, on="id", how="anti")

How it works

  1. join(on='id') defaults to an inner join.
  2. how='left' and how='full' keep unmatched rows.
  3. how='anti' keeps rows with no match.

Keywords and builtins used here

The run, in numbers

Lines
16
Characters to type
354
Tokens
150
Three-star pace
110 tpm

At the three-star pace of 110 tokens a minute, this run takes about 82 seconds.

Type this snippet

Step 1 of 5 in Reshaping & joining, step 15 of 32 in Polars.

← Previous Next →