pythonintermediate

Polars Join Strategies

Perform inner, left, cross, and anti joins in Polars with optimal join strategies.

python
import polars as pl

orders = pl.DataFrame({'order_id':[1,2,3,4],'customer_id':[10,20,10,30],'amount':[100.0,200.0,50.0,75.0]})
customers = pl.DataFrame({'customer_id':[10,20],'name':['Alice','Bob']})

# Inner join
inner = orders.join(customers, on='customer_id', how='inner')

# Left join (keep all orders)
left = orders.join(customers, on='customer_id', how='left')

# Anti join (orders without customer record)
anti = orders.join(customers, on='customer_id', how='anti')

print('Inner:', inner.shape)
print('Left:', left.shape)
print('Anti:', anti.shape)

Use Cases

  • data enrichment
  • orphan record detection
  • ETL joins

Tags

Related Snippets

Similar patterns you can reuse in the same workflow.