dataframes
pandas
data analysis
python
merge

How do I combine two dataframes?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In pandas, "combine two dataframes" can mean several different operations. The right tool depends on whether you want to stack rows, line up columns by index, or join records based on a shared key.

Use concat when the tables already have the same shape

pd.concat is the simplest option when the two dataframes already represent the same kind of data and you just want to append them.

python
1import pandas as pd
2
3sales_q1 = pd.DataFrame(
4    {"region": ["East", "West"], "revenue": [120, 140]}
5)
6
7sales_q2 = pd.DataFrame(
8    {"region": ["East", "West"], "revenue": [135, 155]}
9)
10
11combined = pd.concat([sales_q1, sales_q2], ignore_index=True)
12print(combined)

That produces one longer dataframe. ignore_index=True is usually helpful because it creates a clean sequential index instead of preserving duplicate row labels.

You can also concatenate horizontally:

python
1left = pd.DataFrame({"region": ["East", "West"]})
2right = pd.DataFrame({"manager": ["Nina", "Jared"]})
3
4combined = pd.concat([left, right], axis=1)
5print(combined)

This aligns rows by index rather than by a named column.

Use merge when you have a join key

If both dataframes share a column such as customer_id, use merge. This is the pandas equivalent of a SQL join.

python
1customers = pd.DataFrame(
2    {"customer_id": [1, 2, 3], "name": ["Ava", "Liam", "Mia"]}
3)
4
5orders = pd.DataFrame(
6    {"customer_id": [1, 1, 3], "order_total": [40, 75, 22]}
7)
8
9result = customers.merge(orders, on="customer_id", how="left")
10print(result)

The how argument controls the join type:

  • 'inner keeps only matching keys.'
  • 'left keeps every row from the left dataframe.'
  • 'right keeps every row from the right dataframe.'
  • 'outer keeps all keys from both sides.'

If the key names differ, use left_on= and right_on=:

python
1result = customers.merge(
2    orders.rename(columns={"customer_id": "id"}),
3    left_on="customer_id",
4    right_on="id",
5    how="inner",
6)

Use join when the index is the key

DataFrame.join is convenient when the relationship is already represented by the index:

python
1employees = pd.DataFrame(
2    {"name": ["Ava", "Liam"]},
3    index=[101, 102],
4)
5
6salaries = pd.DataFrame(
7    {"salary": [90000, 85000]},
8    index=[101, 102],
9)
10
11result = employees.join(salaries)
12print(result)

This is effectively an index-based merge and can be very readable when your data is already indexed correctly.

Choosing the right operation

A good rule of thumb is:

  • Use concat to stack or align whole tables.
  • Use merge to join by one or more columns.
  • Use join when the index already represents the relationship.

If the wrong method still appears to "work," inspect the output carefully. Pandas will happily align on indices or keys in ways that may not match your intent.

Common Pitfalls

The most common problem is accidental many-to-many joins. If both dataframes contain repeated values in the join column, merge creates every combination of those matches, which can multiply the row count unexpectedly.

Another issue is mismatched data types. A key column of integers in one dataframe and strings in the other will not join correctly even if the values look the same to a human reader.

concat(axis=1) is also easy to misuse. It aligns by index, not by row position after sorting or filtering, so two dataframes with different indices may produce unexpected NaN values.

Finally, check for duplicate column names. merge adds suffixes such as _x and _y when names overlap, which is a sign you may need to rename columns before joining.

Summary

  • Use pd.concat to append rows or line up whole dataframes by index.
  • Use DataFrame.merge for SQL-style joins on shared columns.
  • Use DataFrame.join when the join key is already the index.
  • Match key data types before joining and inspect the row count afterward.
  • If the result looks odd, check the join type, key uniqueness, and index alignment first.

Course illustration
Course illustration

All Rights Reserved.