Concatenating DataFrames (pd.concat)0%

Concatenating DataFrames (pd.concat)

Beginner12 min read•Updated: Jul 11, 2026
Study MaterialsCheatsheet

Concatenating DataFrames (pd.concat)

Reading Time: 8 Mins | Level: Beginner to Advanced


1. What is Concatenation?

Concatenation is the operation of stitching two or more DataFrames together along an axis—either stacking them vertically (adding more rows) or pasting them horizontally (adding more columns).

In Pandas, this is executed via pd.concat().


2. Vertical Concatenation: Stacking Rows (axis=0)

The most common use case is combining monthly or regional files that share the same schema:

Python
import pandas as pd
 
# Monthly sales reports
df_jan = pd.DataFrame({'Emp': ['Aarav', 'Priya'], 'Sales': [45000, 52000]})
df_feb = pd.DataFrame({'Emp': ['Rohan', 'Sneha'], 'Sales': [48000, 61000]})
 
# Stack rows vertically
# CRITICAL TIP: Always use ignore_index=True to reset row numbering to 0, 1, 2, 3!
df_q1 = pd.concat([df_jan, df_feb], axis=0, ignore_index=True)
print(df_q1)

Output:

Output
Emp Sales
0 Aarav 45000
1 Priya 52000
2 Rohan 48000
3 Sneha 61000

3. Horizontal Concatenation: Pasting Columns (axis=1)

When you have matching rows and want to attach additional feature columns:

Python
df_scores = pd.DataFrame({'Math': [92, 85], 'Science': [88, 90]})
df_info = pd.DataFrame({'Name': ['Deepak', 'Ananya']})
 
# Combine columns horizontally
df_full = pd.concat([df_info, df_scores], axis=1)
print(df_full)

4. Tracking Data Source with Hierarchical Keys

When combining files from different branches or years, you can preserve their origin using the keys argument:

Python
df_combined = pd.concat([df_jan, df_feb], keys=['Jan_2026', 'Feb_2026'])
print(df_combined)

Interactive Quiz

Multiple Choice Questions

Question 1
Which function is used in Pandas to concatenate multiple DataFrames together along an axis?
Question 2
What does ignore_index=True do in pd.concat([df1, df2], ignore_index=True)?
Question 3
Which parameter controls whether concatenation happens by rows (vertically) or by columns (horizontally)?
Question 4
What happens when concatenating two DataFrames vertically if one DataFrame has a column that the other lacks?
Question 5
How can you identify the origin of each row when concatenating multiple DataFrames?

Next Lesson

Relational Joins with pd.merge (Inner, Left, Right, Outer)

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

PrevNext