

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 3
click for more info
Not enough gems
Cost: 6 gems
1: Data Merging
incomplete
2: Inner and Left Joins
incomplete
3: Outer Joins
incomplete
4: Merging on Different Keys
incomplete
5: Merging on Composite Keys
incomplete
6: Handling Column Name Conflicts
incomplete
7: Understanding Cardinality
incomplete
8: Merge Validation
incomplete
9: Finding Unmatched Records
incomplete
10: Multi-Table Joins
incomplete
11: Concatenating DataFrames
incomplete
12: Standardizing Schemas
incomplete
13: Building an Integration Pipeline
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
Cardinality is a mathematical term that has somewhat different meanings in different contexts. When talking about merging datasets, cardinality describes how many times each key appears in each table. It's a way to understand whether a merge will preserve rows or create extras.
A key is the column, or set of columns, used to link rows between datasets during a merge.
When you have duplicate keys, joins can create extra rows. If product CHL-482 appears twice in your products table and once in your sales table, that single sale will match both product rows and turn into two rows. If both tables have duplicates for the same key, the duplicate explosion gets much worse.
That's why cardinality matters: it tells you whether repeated keys are expected or not.
Click to play video
Each key appears once in both tables.
sales: CHL-482, TST-731, OVN-205 (each appears once)
products: CHL-482, TST-731, OVN-205 (each appears once)
This is the easiest case, since there will be no duplicates after merging.
Each key appears once in the left table, but can repeat in the right table.
sales: CHL-482, TST-731, OVN-205 (each appears once)
products: CHL-482, CHL-482, TST-731, OVN-205 (CHL-482 appears twice)
Both sides have duplicates.
sales: CHL-482, CHL-482, TST-731 (CHL-482 appears twice)
products: CHL-482, CHL-482, TST-731 (CHL-482 appears twice)
This is dangerous. 2 sales × 2 product rows = 4 rows for product CHL-482!
# 3 rows, CHL-482 appears twice
sales = pd.DataFrame(
{"product_id": ["CHL-482", "CHL-482", "TST-731"], "quantity": [10, 15, 20]}
)
# 3 rows, CHL-482 appears twice
products = pd.DataFrame(
{"product_id": ["CHL-482", "CHL-482", "TST-731"], "price": [100, 105, 200]}
)
merged = pd.merge(sales, products, on="product_id", how="left")
print(merged)
# product_id quantity price
# 0 CHL-482 10 100
# 1 CHL-482 10 105
# 2 CHL-482 15 100
# 3 CHL-482 15 105
# 4 TST-731 20 200
We started with 3 sales rows and got 5 merged rows, with 4 of them representing a single product ID.
Hover a sales row in the widget below to watch every result row it produced. Because CHL-482 appears twice on each side, that single key explodes into four result rows.
Interactive example available with JavaScript enabled.
Many-to-many relationships exist in real data, and they aren't automatically wrong. The problem is joining on them carelessly. You can create duplicate rows that are hard to clean up later.
If you're seeing duplicate keys where there shouldn't be any (a bad export, a key that isn't actually unique, etc.), that's not a Pandas problem. It's a data quality problem worth addressing.