How to merge multiple dataframes
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
Merging multiple pandas DataFrames usually means combining them either by key columns or by index. The right tool depends on whether you are doing a relational join, stacking rows, or aligning tables side by side.
Repeated merge for key-based joins
If several DataFrames share a common key, the most direct approach is to merge them one by one.
This pattern is explicit and easy to read, especially when each merge uses different join keys or join types.
Using functools.reduce for many DataFrames
If you have a list of DataFrames with the same join key, reduce can keep the code compact.
This is useful when the number of DataFrames is dynamic, but it is slightly harder to debug than explicit step-by-step merges.
When concat is the right tool
Do not use merge if you are simply stacking DataFrames with the same columns. Use pd.concat instead.
concat is for appending along rows or columns. merge is for SQL-like joins on keys.
Merging on index
Sometimes the shared key is already the index. In that case, join on the index directly.
join is especially convenient when several tables are already indexed the same way.
Choosing the join type
When merging multiple tables, how matters a lot:
- '
innerkeeps only keys present in both sides' - '
leftkeeps all keys from the left DataFrame' - '
outerkeeps the union of all keys'
If you merge several DataFrames in sequence, the join type at each step affects the final row set. That is why multi-merge pipelines can silently shrink or expand more than expected.
Watch for duplicate rows
If the key is not unique in one or more DataFrames, each merge can multiply rows. For example, a customer table merged with an orders table produces one row per order, not one row per customer.
This is correct relational behavior, but it surprises people who expected a one-to-one join.
You can validate key shape before merging:
That kind of guard is worth adding in data pipelines.
Common Pitfalls
- Using
mergewhenconcatis the right operation. - Forgetting that sequential merges can duplicate rows if the join key is not unique.
- Mixing join types without checking how they affect the final row count.
- Assuming all DataFrames have the same key name when they do not.
- Writing a compact
reduceexpression before confirming the simpler step-by-step merge works.
Summary
- Use repeated
mergecalls when joining multiple DataFrames on key columns. - Use
functools.reducewhen the number of DataFrames is dynamic and the join logic is uniform. - Use
pd.concatfor stacking DataFrames, not for relational joins. - Use
joinwhen the merge key is already the index. - Always check whether your keys are unique before assuming the merge shape will stay one-to-one.
Related reading
- How to merge multiple feature vectors in DataFrame?
- How to normalize a confusion matrix?
- How to normalize a numpy array to a unit vector
- How to normalize a NumPy array to within a certain range?
- How to mock an import
- How to modify list entries during for loop?
- How to obtain features' weights
- How to pass another entire column as argument to pandas fillna
.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.