</>
Skip to content
Pandas lessons (24/42)

Pandas — Concatenation

Basic concat

import pandas as pd

df1 = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})
df2 = pd.DataFrame({'A': [5, 6], 'B': [7, 8]})

# Vertical (rows)
result = pd.concat([df1, df2])

# Horizontal (columns)
result = pd.concat([df1, df2], axis=1)

With keys

result = pd.concat([df1, df2], keys=['df1', 'df2'])

Ignore index

result = pd.concat([df1, df2], ignore_index=True)

Handle mismatched columns

df3 = pd.DataFrame({'A': [1, 2], 'C': [5, 6]})
result = pd.concat([df1, df3], join='inner')

Mini Practice

  1. Concat vertically
  2. Concat horizontally
  3. Add keys
  4. Handle mismatched columns

Up Next

Continue with Reshape - Reshaping data.

Related Topics

Frequently Asked Questions about Concatenation

What is Concatenation in Pandas?

Concatenation is a fundamental concept in Pandas. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn Concatenation?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Concatenation.

Why is Concatenation important in Pandas?

Concatenation is essential for Pandas development. Understanding this concept will help you write better code and solve real-world problems more effectively.