Python
Pandas
DataFrame
Data Manipulation
Data Analysis

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:

python
1import pandas as pd
2
3orders = pd.DataFrame({
4    "user_id": [1, 2, 3],
5    "order_total": [120, 80, 150],
6})
7
8users = pd.DataFrame({
9    "user_id": [1, 2, 3],
10    "region": ["NA", "EU", "APAC"],
11    "vip": [True, False, True],
12})
13
14result = orders.merge(users[["user_id", "region", "vip"]], on="user_id", how="left")
15print(result)

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:

python
region_map = users.set_index("user_id")["region"]
orders["region"] = orders["user_id"].map(region_map)
print(orders)

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:

python
1left = pd.DataFrame({"sales": [10, 20, 30]}, index=["a", "b", "c"])
2right = pd.DataFrame({"target": [12, 18, 35]}, index=["a", "b", "c"])
3
4left["target"] = right["target"]
5left["gap"] = left["sales"] - left["target"]
6print(left)

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:

python
result["is_high_value"] = result["order_total"] >= 100
result["priority_score"] = result["order_total"] * result["vip"].astype(int)
print(result)

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:

python
1result = orders.merge(
2    users[["user_id", "region"]],
3    on="user_id",
4    how="left",
5    validate="one_to_one"
6)

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.

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
Basis Lab
No 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 course
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