Data Manipulation in Pandas: Concat, Append, and Merge Demystifying

DataGeeks
3 min readJan 9, 2024

Pandas, a powerful data manipulation library in Python, provides a variety of tools for combining and merging datasets. Understanding the differences between concat, append, and merge is crucial for efficiently manipulating and analyzing data. In this blog post, we'll delve into these functions and provide practical examples to illustrate their usage.

Last night when I was working on some industry use cases I struggled a bit to differentiate how and when to use concat, append, and merge . So decided to write the article about this topic.

concat: Combining DataFrames along an Axis

The concat function in Pandas is used to concatenate two or more DataFrames along a particular axis. It is particularly useful when you have DataFrames with the same columns and want to stack them vertically or horizontally.

import pandas as pd
# Example 1: Concatenating vertically
df1 = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})
df2 = pd.DataFrame({'A': [5, 6], 'B': [7, 8]})
result_vertical = pd.concat([df1, df2], axis=0)
-----------output------------
A B
0 1 3
1 2 4
0 5 7
1 6 8

# Example 2: Concatenating horizontally
df3 = pd.DataFrame({'C': [9, 10], 'D': [11, 12]})
result_horizontal = pd.concat([df1, df3], axis=1)
-----------output------------
A…

--

--

DataGeeks

A data couple, Having 15 years of combined experience in data and love to share the knowledge about Data