Merge Datasets using Python

Merging of Datasets in Data Science is the process of combining multiple datasets into a single, unified dataset. It is done to create a more comprehensive and complete dataset for analysis. If you want to learn the techniques used for merging datasets and how to implement them, this article is for you. In this article, I’ll take you through a complete step-by-step guide on how to merge datasets using Python.

A Guide to Merge Datasets Using Python

In Data Science, merging datasets is a common task that can be achieved using various techniques. The most common techniques include:

  1. Concatenation: Merging datasets along an axis (either rows or columns).
  2. Merging: Combining datasets based on common columns (similar to SQL joins).
  3. Joining: Aligning datasets by their indices (rows).

Let’s explore all these merging techniques one by one with their implementation using Python.

Technique 1: Concatenation

Concatenation is used when you want to merge datasets along a particular axis (usually rows or columns) without considering common columns or keys. It’s like stacking datasets on top of each other or side by side. There are two methods for concatenation:

  • Horizontal Concatenation: This involves stacking datasets on top of each other, essentially combining them by rows.
  • Vertical Concatenation: In this case, datasets are merged side by side to add columns to an existing dataset.

Let’s create two sample datasets to implement concatenation:

import pandas as pd
# Sample dataset 1: Sales data for different months
data1 = {'Month': ['Jan', 'Feb', 'Mar'],
         'Sales': [1000, 1200, 800]}
df1 = pd.DataFrame(data1)

# Sample dataset 2: Sales data for different products
data2 = {'Product': ['A', 'B', 'C'],
         'Sales': [500, 600, 400]}
df2 = pd.DataFrame(data2)

print(df1)
  Month  Sales
0   Jan   1000
1   Feb   1200
2   Mar    800
print(df2)
  Product  Sales
0       A    500
1       B    600
2       C    400

Now, here’s how you can merge datasets using Python by implementing the concatenation technique:

# Concatenate along rows (stack them on top of each other)
result_row = pd.concat([df1, df2], axis=0)
print(result_row)
  Month  Sales Product
0   Jan   1000     NaN
1   Feb   1200     NaN
2   Mar    800     NaN
0   NaN    500       A
1   NaN    600       B
2   NaN    400       C
# Concatenate along columns (side by side)
result_column = pd.concat([df1, df2], axis=1)
print(result_column)
  Month  Sales Product  Sales
0   Jan   1000       A    500
1   Feb   1200       B    600
2   Mar    800       C    400

Technique 2: Merging

Merging is used to merge datasets based on common columns or keys. It’s similar to performing SQL JOIN operations. There are four methods used in the merging techniques:

  • Inner Join: Combines two datasets based on a common key or column, retaining only the rows where there’s a match in both datasets.
  • Outer Join: Combines datasets based on a common key, retaining all rows from both datasets. Missing values are filled with NaN or a specified placeholder.
  • Left Join: Joins datasets based on a common key, retaining all rows from the left dataset and matching rows from the right dataset.
  • Right Join: Similar to a left join but retains all rows from the right dataset and matching rows from the left dataset.

Let’s create two sample datasets to implement merging:

# Sample dataset 1: Customer information
data1 = {'CustomerID': [1, 2, 3],
         'Name': ['Alice', 'Bob', 'Charlie']}
df1 = pd.DataFrame(data1)
print(df1)
   CustomerID     Name
0           1    Alice
1           2      Bob
2           3  Charlie
# Sample dataset 2: Purchase history
data2 = {'CustomerID': [2, 3, 4],
         'Product': ['A', 'B', 'C']}
df2 = pd.DataFrame(data2)
print(df2)
   CustomerID Product
0           2       A
1           3       B
2           4       C

Now, here’s how you can merge datasets using Python by implementing the merging technique:

# Inner join based on the 'CustomerID' column
result_inner = pd.merge(df1, df2, on='CustomerID', how='inner')
print(result_inner)
   CustomerID     Name Product
0           2      Bob       A
1           3  Charlie       B
# Left join to include all rows from df1
result_left = pd.merge(df1, df2, on='CustomerID', how='left')
print(result_left)
   CustomerID     Name Product
0           1    Alice     NaN
1           2      Bob       A
2           3  Charlie       B
# Right join to include all rows from df2
result_right = pd.merge(df1, df2, on='CustomerID', how='right')
print(result_right)
   CustomerID     Name Product
0           2      Bob       A
1           3  Charlie       B
2           4      NaN       C
# Outer join to include all rows from both datasets
result_outer = pd.merge(df1, df2, on='CustomerID', how='outer')
print(result_outer)
   CustomerID     Name Product
0           1    Alice     NaN
1           2      Bob       A
2           3  Charlie       B
3           4      NaN       C

Technique 3: Joining

Joining is similar to merging but is primarily used when you have multiple DataFrames with a common index. It combines datasets based on index labels. Just like the merging technique, the joining technique also has four methods: Inner Join, Outer Join, Left Join, and Right Join.

Let’s create two sample datasets to implement joining:

# Sample dataset 1: Student names and scores
data1 = {'Name': ['Alice', 'Bob', 'Charlie'],
         'Score': [85, 92, 78]}
df1 = pd.DataFrame(data1, index=['A', 'B', 'C'])
print(df1)
      Name  Score
A    Alice     85
B      Bob     92
C  Charlie     78
# Sample dataset 2: Student grades
data2 = {'Grade': ['A', 'B', 'C'],
         'Status': ['Pass', 'Pass', 'Fail']}
df2 = pd.DataFrame(data2, index=['A', 'B', 'D'])
print(df2)
  Grade Status
A     A   Pass
B     B   Pass
D     C   Fail

Now, here’s how you can merge datasets using Python by implementing the joining technique:

# Inner join based on the common index
result_inner = df1.join(df2, how='inner')
print(result_inner)
    Name  Score Grade Status
A  Alice     85     A   Pass
B    Bob     92     B   Pass
# Left join to include all rows from df1
result_left = df1.join(df2, how='left')
print(result_left)
      Name  Score Grade Status
A    Alice     85     A   Pass
B      Bob     92     B   Pass
C  Charlie     78   NaN    NaN
# Right join to include all rows from df2
result_right = df1.join(df2, how='right')
print(result_right)
    Name  Score Grade Status
A  Alice   85.0     A   Pass
B    Bob   92.0     B   Pass
D    NaN    NaN     C   Fail
# Outer join to include all rows from both datasets
result_outer = df1.join(df2, how='outer')
print(result_outer)
      Name  Score Grade Status
A    Alice   85.0     A   Pass
B      Bob   92.0     B   Pass
C  Charlie   78.0   NaN    NaN
D      NaN    NaN     C   Fail

So, these are the fundamental techniques for merging datasets in Data Science.

Summary

So, merging datasets is a common task that can be achieved using various techniques. The most common techniques include:

  1. Concatenation: Merging datasets along an axis (either rows or columns).
  2. Merging: Combining datasets based on common columns (similar to SQL joins).
  3. Joining: Aligning datasets by their indices (rows).

I hope you liked this article on how to Merge Datasets using Python. Feel free to ask valuable questions in the comments section below.

Aman Kharwal
Aman Kharwal

AI/ML Engineer | Published Author. My aim is to decode data science for the real world in the most simple words.

Articles: 2190

Leave a Reply

Discover more from AmanXai by Aman Kharwal

Subscribe now to keep reading and get access to the full archive.

Continue reading