Python
Pandas
DataFrame
Data Analysis
Merging Columns

Python Pandas merge only certain columns

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

In Pandas, merging whole tables is easy, but production data workflows often need only a few columns from the right side table. Selecting only required columns improves readability, reduces memory usage, and avoids accidental duplicate fields. A clean merge pattern is to subset first, then join with explicit key columns.

Basic Pattern: Select Columns Before Merge

Suppose you have a customer table and a revenue table, but you only need two fields from revenue.

python
1import pandas as pd
2
3customers = pd.DataFrame({
4    'customer_id': [1, 2, 3, 4],
5    'name': ['Ana', 'Ben', 'Cia', 'Dan'],
6    'country': ['CA', 'US', 'CA', 'US']
7})
8
9revenue = pd.DataFrame({
10    'customer_id': [1, 2, 2, 4],
11    'revenue': [1200, 900, 300, 1500],
12    'currency': ['CAD', 'USD', 'USD', 'USD'],
13    'sales_rep': ['r1', 'r2', 'r2', 'r3']
14})
15
16subset = revenue[['customer_id', 'revenue']]
17merged = customers.merge(subset, on='customer_id', how='left')
18print(merged)

This keeps only needed fields in the result and avoids carrying currency and sales_rep when unnecessary.

Handle One to Many Keys Carefully

If the right table has multiple rows per key, merge duplicates rows on the left side. Sometimes that is desired, but often you want one row per key.

Aggregate first if needed.

python
1agg_revenue = (
2    revenue
3    .groupby('customer_id', as_index=False)['revenue']
4    .sum()
5)
6
7result = customers.merge(agg_revenue, on='customer_id', how='left')
8print(result)

Always decide whether one to many expansion is correct for your business logic.

Merge with Different Key Names

When key names differ, use left_on and right_on while still selecting columns explicitly.

python
1orders = pd.DataFrame({
2    'order_id': [10, 11, 12],
3    'cust_id': [1, 2, 5]
4})
5
6profile = pd.DataFrame({
7    'customer_id': [1, 2, 3],
8    'tier': ['gold', 'silver', 'bronze'],
9    'region': ['east', 'west', 'north']
10})
11
12right_subset = profile[['customer_id', 'tier']]
13joined = orders.merge(
14    right_subset,
15    left_on='cust_id',
16    right_on='customer_id',
17    how='left'
18).drop(columns=['customer_id'])
19
20print(joined)

Dropping the duplicate key column keeps result clean.

Avoid Column Name Collisions

If both tables contain same column names, Pandas adds suffixes. This may be useful, but explicit renaming is often clearer.

python
1left = pd.DataFrame({'id': [1, 2], 'status': ['new', 'old']})
2right = pd.DataFrame({'id': [1, 2], 'status': ['active', 'inactive']})
3
4merged = left.merge(right[['id', 'status']], on='id', how='left', suffixes=('_left', '_right'))
5print(merged)

Explicit suffixes prevent surprise column overwrites and make downstream code stable.

Performance Tips for Large DataFrames

For large joins, performance and memory matter.

  • Select only required columns from both sides
  • Ensure key columns share same dtype before merge
  • Use categorical dtype for low cardinality string keys where appropriate
  • Validate join size with sampled runs before full dataset

dtype mismatch example:

python
orders['cust_id'] = orders['cust_id'].astype('int64')
profile['customer_id'] = profile['customer_id'].astype('int64')

Matching dtypes avoids silent merge misses and costly conversions.

Validation Checks After Merge

Add assertions after merge to catch data quality issues early.

python
1out = customers.merge(subset, on='customer_id', how='left')
2
3# Check expected row count preservation
4assert len(out) == len(customers), 'unexpected row expansion'
5
6# Check missing join rates
7missing_ratio = out['revenue'].isna().mean()
8print('missing revenue ratio:', round(missing_ratio, 3))

Validation is especially useful in ETL jobs where upstream schemas may change.

Common Pitfalls

A common mistake is selecting right side columns after merge instead of before merge. This can increase memory and make joins slower on large tables.

Another issue is forgetting duplicate keys in the right table. The output row count increases unexpectedly, breaking downstream assumptions.

A third issue is key dtype mismatch, such as strings on one side and integers on the other. This yields many null matches and can look like missing data.

Teams also often ignore post merge validation, so join regressions remain hidden until reporting errors appear.

Summary

  • Subset right table columns before merge for clarity and efficiency
  • Handle one to many keys intentionally with pre aggregation when needed
  • Use explicit key mapping and suffix rules to control output schema
  • Align key dtypes to avoid silent join failures
  • Add row count and null rate checks after merge in production pipelines

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.