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
join(on='id')defaults to an inner join.how='left'andhow='full'keep unmatched rows.how='anti'keeps rows with no match.
Keywords and builtins used here
as
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.
Step 1 of 5 in Reshaping & joining, step 15 of 32 in Polars.