Add new columns to a pandas DataFrame from another DataFrame
Codemia Knowledge Hub ·
Adding columns to one pandas DataFrame from another is a join, not a loop. Use merge when the tables share a key column, map when you need a single lookup column, and direct assignment or join when the indexes already match. These vectorized options are much faster and safer than iterating row by row.
Use merge for key columns
If the DataFrames share a key such as user_id, merge is the clearest way to bring over one or more columns from a lookup table:
This adds region and vip to orders, and the left join keeps every row of orders.
Use map for a single lookup column
For one new column with a one-to-one relationship, map is simpler than a full merge:
It works best when the lookup DataFrame has unique keys.
Use index alignment when the index already matches
If both DataFrames share the same index, direct assignment or join is enough:
Pandas aligns by index labels, not row position. That is powerful, but mismatched indexes can silently produce missing values.
Derive new columns after the join
Once the external columns are attached, compute derived fields with vectorized expressions:
This enrich-then-compute pattern is common in feature engineering and reporting.
Validate join assumptions
Check whether the relationship is one-to-one, one-to-many or many-to-many before merging. Duplicate keys in the lookup table can create more rows than you expect. validate makes the assumption explicit:
It can save a lot of debugging when a supposedly unique key is not unique.
Mistakes that produce wrong or missing values
- Using a Python loop and
iterrows()for what is really a join. It is slower and harder to reason about. - Forgetting that pandas aligns by index labels, which can give unexpected missing values during direct assignment.
- Duplicate keys, which make a merge multiply rows instead of only adding columns.
- Mismatched key types. A string key in one DataFrame and an integer key in the other will not match, even if the values look the same when printed.
Related reading
- Add single element to array in numpy
- Add trend line to pandas
- Add x and y labels to a pandas plot
- Adding a matplotlib legend
- Add variables to tuple
- Adding a method to an existing object instance in Python
- Adding a y-axis label to secondary y-axis in matplotlib
- Adding custom jars to pyspark in jupyter notebook
System design interview problems on this topic
- Design a Web Crawler: solution and walkthrough
- Design a Task Scheduler: solution and walkthrough
- Design a URL Shortener (TinyURL): solution and walkthrough
- Design an API Rate Limiter: solution and walkthrough
.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 courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.