Hiring Partners
Segilola Resources Operating LimitedTaviatech TechnologiesFCMBMTN NigeriaAdvans La Fayette Microfinance BankAboki AfricaFirst Bank of NigeriaOPay
โ† Basic Data Transformation ยท Lesson 7 of 9

Module Six

๐Ÿ“– Every lesson in this course is free to read right here, no account needed. Create a free account to track your progress, take the exam, and earn your certificate.
7
Module 6: Combining and Merging Data

MODULE 6: COMBINING AND MERGING DATA

Course: Basic Data Transformation

Module: 6 of 8

Level: Beginner

1. Introduction

Imagine that you are helping a school administrator organise student records. The school has two separate spreadsheets.

The first spreadsheet contains students' names and their identification numbers. The second spreadsheet contains their examination scores.

The administrator wants to see each student's name together with their examination score. How can this be done?

One solution is to combine the two sets of information. This process is called combining or merging data.

In this module, you will learn how to bring information from different tables and datasets together.

2. What Is Combining Data?

Combining data means bringing information from two or more sources together into one dataset.

For example, a school may keep student names in one table and examination scores in another table. Combining the information makes it easier to understand each student's performance.

Example: Two Separate Tables

Table A: Student Information

Student IDName
101Tunde
102Amina
103David

Table B: Examination Scores

Student IDScore
10185
10290
10375

Both tables have something in common: Student ID. We can use this information to bring the records together.

Combined Table

Student IDNameScore
101Tunde85
102Amina90
103David75

Now we can easily see each student's name and examination score in one table.

3. Why Do We Combine Data?

Combining data helps us to:

  • Bring information from different sources together.
  • Create complete records.
  • Make data analysis easier.
  • Compare information from different departments.
  • Reduce the need to check many separate tables.
  • Prepare information for reports and decision-making.

Real-Life Examples

  • Schools: Combining student details with examination results.
  • Hospitals: Combining patient information with appointment records.
  • Banks: Combining customer details with transaction records.
  • Businesses: Combining product details with sales records.
  • Online learning platforms: Combining learner profiles with course progress.

4. Understanding Data Merging

Data merging means joining two or more datasets using a common piece of information.

This common information is called a key.

For example, Student ID can be used as a key because it identifies each student in both tables.

Example

Imagine these two tables:

Student IDName
201Chika
202Samuel
Student IDCourse
201Python
202Data Analysis

Student ID is the common key. We can use it to connect the two tables.

5. Understanding Common Types of Data Merging

There are different ways to merge datasets. Four common types are inner join, left join, right join, and outer join.

A. Inner Join

An inner join keeps only records that have matching keys in both tables.

Imagine these tables:

Table A

IDName
1Tunde
2Amina
3David

Table B

IDScore
180
290
470

The inner join produces:

IDNameScore
1Tunde80
2Amina90

David is not included because his ID does not appear in Table B. ID 4 is also excluded because it does not appear in Table A.

B. Left Join

A left join keeps every record from the left table and adds matching information from the right table.

Using our previous example:

IDNameScore
1Tunde80
2Amina90
3DavidNo matching score

David remains in the result because he was in the left table. His score is missing because no matching record was found.

C. Right Join

A right join keeps every record from the right table and adds matching information from the left table.

Using our example:

IDNameScore
1Tunde80
2Amina90
4No matching name70

D. Outer Join

An outer join keeps all records from both tables. Where there is no matching record, the missing information is left empty.

IDNameScore
1Tunde80
2Amina90
3DavidMissing
4Missing70
Remember:
  • Inner join: only matching records.
  • Left join: all records from the left table.
  • Right join: all records from the right table.
  • Outer join: all records from both tables.

6. Combining Data Using Python

Python can combine information from different datasets. One popular tool for this task is called Pandas.

Pandas is a Python library that helps people organise, clean, analyse, and transform data.

Step 1: Import Pandas

import pandas as pd

This tells Python that we want to use the Pandas library.

Step 2: Create Two Datasets

import pandas as pd

students = {
    "ID": [101, 102, 103],
    "Name": ["Tunde", "Amina", "David"]
}

scores = {
    "ID": [101, 102, 103],
    "Score": [85, 90, 75]
}

students_df = pd.DataFrame(students)
scores_df = pd.DataFrame(scores)

print(students_df)
print(scores_df)

We have created two separate datasets: one contains student names and the other contains examination scores.

Step 3: Merge the Datasets

combined = pd.merge(
    students_df,
    scores_df,
    on="ID"
)

print(combined)

Expected Output:

    ID    Name  Score
0  101   Tunde     85
1  102   Amina     90
2  103   David     75

The two datasets have been merged using the ID column.

7. Performing Different Types of Joins with Pandas

Pandas allows us to choose how two datasets should be merged. We do this using the how parameter.

Inner Join

result = pd.merge(
    students_df,
    scores_df,
    on="ID",
    how="inner"
)

print(result)

Left Join

result = pd.merge(
    students_df,
    scores_df,
    on="ID",
    how="left"
)

print(result)

Right Join

result = pd.merge(
    students_df,
    scores_df,
    on="ID",
    how="right"
)

print(result)

Outer Join

result = pd.merge(
    students_df,
    scores_df,
    on="ID",
    how="outer"
)

print(result)

The join type determines which records are kept in the final dataset.

8. Combining Data by Adding Rows

Sometimes you do not need to match records using an ID. Instead, you may want to place one dataset below another.

This process is called concatenation.

Example

First dataset:

class_one = pd.DataFrame({
    "Name": ["Tunde", "Amina"],
    "Score": [80, 90]
})

Second dataset:

class_two = pd.DataFrame({
    "Name": ["David", "Grace"],
    "Score": [75, 85]
})

Combine them:

all_students = pd.concat(
    [class_one, class_two],
    ignore_index=True
)

print(all_students)

Expected Output:

    Name  Score
0  Tunde     80
1  Amina     90
2  David     75
3  Grace     85

The records from both classes are now in one dataset.

9. Understanding the Difference Between Merging and Concatenation

MergingConcatenation
Connects datasets using common columns or keys. Places datasets together along rows or columns.
Useful when two tables contain related information. Useful when combining similar datasets.
Example: Connecting names to examination scores. Example: Combining Class One and Class Two records.

10. Practical Project: Combining Student Records

Your Challenge

A secondary school in Ibadan has two datasets. The first contains student details, while the second contains their examination scores.

Your task is to combine the information into one complete dataset.

Student Information:

students = {
    "ID": [201, 202, 203, 204],
    "Name": ["Chika", "Samuel", "Grace", "Tunde"]
}

Examination Scores:

scores = {
    "ID": [201, 202, 203, 204],
    "Score": [88, 76, 95, 82]
}

Instructions

  1. Import the Pandas library.
  2. Create the student information dataset.
  3. Create the examination scores dataset.
  4. Convert both datasets into DataFrames.
  5. Merge the two datasets using the ID column.
  6. Display the final combined table.

Complete Solution

import pandas as pd

students = {
    "ID": [201, 202, 203, 204],
    "Name": ["Chika", "Samuel", "Grace", "Tunde"]
}

scores = {
    "ID": [201, 202, 203, 204],
    "Score": [88, 76, 95, 82]
}

students_df = pd.DataFrame(students)
scores_df = pd.DataFrame(scores)

combined = pd.merge(
    students_df,
    scores_df,
    on="ID",
    how="inner"
)

print(combined)

Expected Output:

    ID    Name  Score
0  201   Chika     88
1  202  Samuel     76
2  203   Grace     95
3  204   Tunde     82

11. Common Mistakes to Avoid

Mistake 1: Using the Wrong Key

When merging datasets, make sure you use the correct common column. For example, using student names instead of student IDs may cause problems if names are duplicated or spelled differently.

Mistake 2: Forgetting to Check Missing Records

Some records may not have matching information. Always inspect the merged dataset for missing values.

Mistake 3: Confusing Merging with Concatenation

Merging connects related records using common information. Concatenation places datasets together along rows or columns.

Mistake 4: Duplicate Records

If a key appears more than once in either dataset, merging may produce more rows than expected. Check your keys and records before merging.

12. Key Terms to Remember

TermMeaning
Combining DataBringing information from different sources together.
Data MergingJoining datasets using common information.
KeyA column used to connect related records.
Inner JoinKeeps only matching records.
Left JoinKeeps all records from the left dataset.
Right JoinKeeps all records from the right dataset.
Outer JoinKeeps records from both datasets.
ConcatenationPlacing datasets together along rows or columns.
PandasA Python library for working with data.
DataFrameA table-like data structure in Pandas.

13. Module Summary

In this module, you learned how to combine information from different datasets.

You learned how to:

  • Understand what combining data means.
  • Identify common keys in datasets.
  • Understand inner, left, right, and outer joins.
  • Use Python and Pandas to merge datasets.
  • Combine datasets using concatenation.
  • Identify common mistakes when combining information.
Remember: Combining data helps us bring related information together so that we can understand it better and make more useful decisions.

14. Module Assessment: Test Your Knowledge

Answer all 10 questions to check what you have learned.

Next Module

Module 7: Handling Missing and Duplicate Data

In the next module, you will learn how to identify missing information, find duplicate records, and prepare datasets for accurate analysis.

CRMNuggets โ€“ Learn. Practice. Get Certified.

๐Ÿ† Get Certified

๐Ÿ”’

Earn this certificate

Every lesson is already free to read. Sign up, finish the lessons, pass the exam (โ‚ฆ2,000 or 20,000 coins), and get a verified certificate with your name on it.

๐ŸŽ“ Sign Up to Get Certified
๐Ÿ› ๏ธ Practice Tools
Hands-on simulators & labs - subscription required.
โ†’
๐ŸŽฏ Internship Tasks
Real-world tasks to build your portfolio - unlock them with a subscription.
โ†’