Sub topic 6
Join keys and overlapping column names
This chapter assumes you know basic Python, can create a Polars DataFrame, and have seen join with how="inner" and how="left". Here you will control which columns are matched and what happens when both tables share a column name that is not a key.
Naming the keys
A join key is a column whose values decide which rows match. Polars gives you three ways to name it:
on="customer_id"when both tables use the same name.on=["region", "product"]when a row is identified by several columns. All of them must match.left_on=andright_on=when the names differ.
Here the orders table calls the key cust, and the customers table calls it customer_id.
import polars as pl
orders = pl.DataFrame({
"order_id": [100, 101, 102],
"cust": [1, 1, 4],
"amount": [30, 20, 50],
})
customers = pl.DataFrame({
"customer_id": [1, 2, 3],
"name": ["Ana", "Ben", "Cy"],
})
out = orders.join(customers, left_on="cust", right_on="customer_id", how="left")
print(out.columns) # ['order_id', 'cust', 'amount', 'name']The result keeps the left key (cust) and drops the right key (customer_id), because the two columns hold the same values on matched rows.
Joining on several columns
With a list of keys, a row matches only when every key column is equal.
sales = pl.DataFrame({
"region": ["east", "east", "west"],
"product": ["a", "b", "a"],
"sales": [10, 20, 5],
})
targets = pl.DataFrame({
"region": ["east", "east", "west"],
"product": ["a", "b", "b"],
"target": [8, 25, 12],
})
out = sales.join(targets, on=["region", "product"], how="inner").sort("region", "product")
print(out) # east/a: 10 vs 8, east/b: 20 vs 25. west/a and west/b have no partner.Matching on region alone would pair every east sale with every east target. Include every column that identifies a row.
Overlapping column names
Polars cannot hold two columns with the same name. If a non-key column appears in both tables, the right-hand one gets the suffix _right. Use suffix to choose a clearer label.
orders = pl.DataFrame({
"order_id": [100, 101],
"cust": [1, 2],
"note": ["gift", "rush"],
})
customers = pl.DataFrame({
"customer_id": [1, 2],
"note": ["vip", "new"],
})
default = orders.join(customers, left_on="cust", right_on="customer_id")
named = orders.join(customers, left_on="cust", right_on="customer_id", suffix="_customer")
print(default.columns) # ['order_id', 'cust', 'note', 'note_right']
print(named.columns) # ['order_id', 'cust', 'note', 'note_customer']The suffix is applied only to right-hand columns whose names collide. The left table's columns keep their names.
Keys in full joins
In a full join, a row can exist on only one side, so Polars keeps both key columns by default. The right key appears as customer_id or customer_id_right, depending on the names, and holds null where the left side has no match. Pass coalesce=True to merge the two key columns into one.
out = orders.join(
customers, left_on="cust", right_on="customer_id", how="full", coalesce=True
)Summary table
| Situation | Argument |
|---|---|
| Same key name in both tables | `on="key"` |
| Composite key | `on=["k1", "k2"]` |
| Different key names | `left_on="a", right_on="b"` |
| Non-key name collision | `suffix="_label"` |
| One key column after a full join | `coalesce=True` |
Exercise
You join orders (columns order_id, cust, amount, date) to shipments (columns order_id, date, carrier) using on="order_id". What is the name of the shipment date column in the result, and how would you make it ship_date?
Show answer
The default name is date_right, because date is not a key and exists in both tables. Pass suffix="_ship" to get date_ship. To get exactly ship_date, rename the column first: shipments.rename({"date": "ship_date"}).