Pandas concat(): How to Combine DataFrames Vertically and Horizontally
Pandas concat() combines two or more Series or DataFrames along a selected axis. It is commonly used to stack datasets from different months, combine data collected from several branches, or place related columns side by side.
This tutorial explains how to use pd.concat() with practical examples, including vertical and horizontal concatenation, index handling, missing columns, hierarchical keys, duplicate indexes and common mistakes.
Related guides: Pandas DataFrames, Pandas merge(), select rows and columns, read CSV files and groupby().
- What does concat() do?
- Create sample DataFrames
- Syntax and key parameters
- Concatenate vertically (rows)
- Use ignore_index=True
- Concatenate horizontally (columns)
- Combine DataFrames with different columns
- Use join='inner' and join='outer'
- Keep track of source data with keys
- Concatenate Series and DataFrames
- Check duplicate indexes
- concat() vs merge()
- Common mistakes
- Exercises with solutions
- Frequently asked questions
1. What does concat() do in Pandas?
pd.concat() joins objects along an axis. By default, axis=0 stacks rows. With axis=1, it places columns side by side and aligns rows by index labels.
| Setting | What it does | Typical use |
|---|---|---|
axis=0 | Stacks rows vertically | Append January records to February records |
axis=1 | Combines columns horizontally, aligning by index | Place two sets of features side by side |
ignore_index=True | Creates a new sequential index on the concatenation axis | Build one clean row index after stacking |
join='outer' | Keeps the union of labels on the other axis | Preserve all columns when schemas differ |
join='inner' | Keeps only labels shared on the other axis | Keep only common columns when stacking |
concat() combines objects along an axis. merge() matches rows using key columns, like a database join. See Pandas merge() for key-based joins.2. Create sample DataFrames
Imagine a shop stores sales in separate DataFrames for two months. Each table has the same columns.
import pandas as pd
january = pd.DataFrame({
"Date": ["2026-01-05", "2026-01-12"],
"Product": ["Notebook", "Pen"],
"Sales": [120, 80]
})
february = pd.DataFrame({
"Date": ["2026-02-03", "2026-02-18"],
"Product": ["Folder", "Notebook"],
"Sales": [65, 140]
})
print(january)
print(february)
To analyse sales across both months, the records can be stacked into one DataFrame.
3. Pandas concat() syntax and parameters
result = pd.concat(objs, axis=0, join="outer", ignore_index=False, keys=None)
| Parameter | Purpose | Example |
|---|---|---|
objs | Iterable of Series or DataFrames to combine | [january, february] |
axis | Direction of concatenation | axis=0 or axis=1 |
join | How labels on the other axis are matched | join='outer' |
ignore_index | Whether to reset the concatenation-axis labels | ignore_index=True |
keys | Adds outer-level labels identifying each input object | keys=['Jan', 'Feb'] |
verify_integrity | Raises an error if the new concatenation axis has duplicate labels | verify_integrity=True |
sort | Controls sorting of the non-concatenation axis in relevant cases | sort=False |
4. Concatenate vertically: stack rows
Vertical concatenation is the most common use of concat(). It places the rows of one DataFrame below the rows of another.
all_sales = pd.concat([january, february])
print(all_sales)
Date Product Sales 0 2026-01-05 Notebook 120 1 2026-01-12 Pen 80 0 2026-02-03 Folder 65 1 2026-02-18 Notebook 140
The original index labels are preserved by default, so the index contains 0 and 1 twice. This is allowed, but you may prefer a fresh index for a combined dataset.
5. Use ignore_index=True
Set ignore_index=True when the original row indexes are not meaningful and you want a new index from zero.
all_sales = pd.concat([january, february], ignore_index=True)
print(all_sales)
Date Product Sales 0 2026-01-05 Notebook 120 1 2026-01-12 Pen 80 2 2026-02-03 Folder 65 3 2026-02-18 Notebook 140
ignore_index=True resets labels on the concatenation axis. When using axis=0, that means row labels. It does not remove duplicate records from the data.6. Concatenate horizontally: combine columns
Use axis=1 to place columns side by side. Pandas aligns the objects using their index labels rather than assuming that the first row always matches the first row.
names = pd.DataFrame({"Name": ["Amina", "Bilal", "Sara"]}, index=[101, 102, 103])
scores = pd.DataFrame({"Score": [88, 91, 85]}, index=[101, 102, 103])
result = pd.concat([names, scores], axis=1)
print(result)
Name Score 101 Amina 88 102 Bilal 91 103 Sara 85
Matching index labels keep each name beside the intended score. If the indexes differ, Pandas aligns by label and may introduce missing values.
7. Combine DataFrames with different columns
When stacking DataFrames that do not have the same columns, the default outer join keeps all columns. Where a row's source DataFrame did not have a particular column, the resulting value is missing.
north = pd.DataFrame({"Product": ["Pen", "Book"], "Sales": [20, 50]})
south = pd.DataFrame({"Product": ["Bag", "Folder"], "Returns": [1, 2]})
combined = pd.concat([north, south], ignore_index=True)
print(combined)
Product Sales Returns 0 Pen 20.0 NaN 1 Book 50.0 NaN 2 Bag NaN 1.0 3 Folder NaN 2.0
The missing values appear because the source tables have different schemas. You can inspect or handle them using Pandas missing-value methods.
8. Use join='inner' and join='outer'
For vertical concatenation, join controls which columns are retained when the input DataFrames have different columns.
north = pd.DataFrame({"Product": ["Pen"], "Sales": [20]})
south = pd.DataFrame({"Product": ["Bag"], "Returns": [1]})
outer_result = pd.concat([north, south], join="outer", ignore_index=True)
inner_result = pd.concat([north, south], join="inner", ignore_index=True)
print("Outer join columns:", outer_result.columns.tolist())
print("Inner join columns:", inner_result.columns.tolist())
Outer join columns: ['Product', 'Sales', 'Returns'] Inner join columns: ['Product']
join='outer' is the default and preserves the union of columns. join='inner' keeps only columns present in every input object, which may discard useful information.
9. Keep track of source data with keys
The keys parameter adds an outer index level that identifies the source DataFrame. This can be useful when combining monthly, regional or departmental data.
combined = pd.concat([january, february], keys=["January", "February"])
print(combined)
The resulting index is a MultiIndex: the outer level identifies the month, and the inner level preserves the original row index. To access one source, use combined.loc["January"].
10. Concatenate Series and DataFrames
You can concatenate Series as well as DataFrames. When combining Series vertically, they are stacked into one Series. When combining them horizontally, each Series becomes a column and its name may be used as the column label.
first = pd.Series([10, 20, 30], name="Sales")
second = pd.Series([2, 4, 6], name="Units")
columns = pd.concat([first, second], axis=1)
print(columns)
For readable output, give Series meaningful names before concatenating them horizontally.
11. Check duplicate index labels
Concatenation can preserve duplicate index labels. If unique labels on the concatenation axis are required, use verify_integrity=True to make Pandas raise an error when duplicates occur.
a = pd.DataFrame({"Value": [10]}, index=[1])
b = pd.DataFrame({"Value": [20]}, index=[1])
# This raises ValueError because the index label 1 is duplicated
# result = pd.concat([a, b], verify_integrity=True)
# If the old index is not important, create a fresh one instead
result = pd.concat([a, b], ignore_index=True)
print(result)
Do not use verify_integrity=True automatically on every dataset. Use it when duplicate labels would indicate a problem in your workflow.
12. concat() vs merge()
| Feature | concat() | merge() |
|---|---|---|
| Main purpose | Stack or align objects along an axis | Match rows using key columns or indexes |
| Typical example | Combine monthly sales files with the same schema | Add customer details to order records by customer ID |
| Common parameters | axis, ignore_index, keys | on, how, left_on, right_on |
Use Pandas merge() when you need to match records by keys. Use concat() when you want to stack or align objects.
13. Common mistakes
Expecting concat() to match records by a key
concat() does not perform a relational join on a customer ID. Use merge() for that task.
Forgetting ignore_index
When stacking DataFrames, repeated index labels may be preserved. Use ignore_index=True if you want a new sequential row index.
Assuming axis=1 matches rows by position
Horizontal concatenation aligns by index labels. Check and align indexes if each row must correspond to a particular observation.
Ignoring missing values after combining different schemas
With the default outer join, absent columns create missing values. Inspect the output with isna().sum() and decide how to handle those values.
Calling concat repeatedly in a loop
Repeatedly growing a DataFrame can be inefficient. Collect objects in a list and concatenate once when practical: pd.concat(frames, ignore_index=True).
14. Exercises with solutions
Exercise 1: Stack two DataFrames
Combine january and february into one DataFrame with a fresh row index.
Show solution
result = pd.concat([january, february], ignore_index=True)
Exercise 2: Combine columns
Concatenate names and scores side by side, aligning by their indexes.
Show solution
result = pd.concat([names, scores], axis=1)
Exercise 3: Keep only common columns
Stack two DataFrames but retain only the columns present in both.
Show solution
result = pd.concat([north, south], join="inner", ignore_index=True)
Exercise 4: Track the source
Combine two DataFrames and label each source as "First" or "Second".
Show solution
result = pd.concat([first_df, second_df], keys=["First", "Second"])
15. Frequently asked questions
What does concat() do in Pandas?
It combines Series or DataFrames along rows or columns. The default is vertical concatenation using axis=0.
How do I combine two DataFrames row by row?
Use pd.concat([df1, df2], ignore_index=True) to stack rows and create a fresh sequential index.
What is the difference between axis=0 and axis=1?
axis=0 stacks rows. axis=1 combines columns and aligns objects by index labels.
Does concat() remove duplicate rows?
No. It combines objects but does not automatically remove duplicate records. Use drop_duplicates() when deduplication is required.
What is the difference between concat() and merge()?
concat() stacks or aligns objects along an axis; merge() matches rows using key columns or indexes.
Why do missing values appear after concat()?
When input DataFrames have different columns or indexes, Pandas aligns labels and inserts missing values where no corresponding value exists.
