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.
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.
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.
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.
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.
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:
Matching dtypes avoids silent merge misses and costly conversions.
Validation Checks After Merge
Add assertions after merge to catch data quality issues early.
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
- python pandas remove duplicate columns
- Python Pandas to_sql, how to create a table with a primary key?
- Python rewrite a looping numpy math function to run on GPU
- python sklearn multiple linear regression display r-squared
- Python pathlib make directories if they don’t exist
- Python PCA on Matrix too large to fit into memory
- Python text processing NLTK and pandas
- python tsne.transform does not exist?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.