Course: Basic Data Transformation
Module: 6 of 8
Level: Beginner
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.
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.
Table A: Student Information
| Student ID | Name |
|---|---|
| 101 | Tunde |
| 102 | Amina |
| 103 | David |
Table B: Examination Scores
| Student ID | Score |
|---|---|
| 101 | 85 |
| 102 | 90 |
| 103 | 75 |
Both tables have something in common: Student ID. We can use this information to bring the records together.
| Student ID | Name | Score |
|---|---|---|
| 101 | Tunde | 85 |
| 102 | Amina | 90 |
| 103 | David | 75 |
Now we can easily see each student's name and examination score in one table.
Combining data helps us to:
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.
Imagine these two tables:
| Student ID | Name |
|---|---|
| 201 | Chika |
| 202 | Samuel |
| Student ID | Course |
|---|---|
| 201 | Python |
| 202 | Data Analysis |
Student ID is the common key. We can use it to connect the two tables.
There are different ways to merge datasets. Four common types are inner join, left join, right join, and outer join.
An inner join keeps only records that have matching keys in both tables.
Imagine these tables:
Table A
| ID | Name |
|---|---|
| 1 | Tunde |
| 2 | Amina |
| 3 | David |
Table B
| ID | Score |
|---|---|
| 1 | 80 |
| 2 | 90 |
| 4 | 70 |
The inner join produces:
| ID | Name | Score |
|---|---|---|
| 1 | Tunde | 80 |
| 2 | Amina | 90 |
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.
A left join keeps every record from the left table and adds matching information from the right table.
Using our previous example:
| ID | Name | Score |
|---|---|---|
| 1 | Tunde | 80 |
| 2 | Amina | 90 |
| 3 | David | No matching score |
David remains in the result because he was in the left table. His score is missing because no matching record was found.
A right join keeps every record from the right table and adds matching information from the left table.
Using our example:
| ID | Name | Score |
|---|---|---|
| 1 | Tunde | 80 |
| 2 | Amina | 90 |
| 4 | No matching name | 70 |
An outer join keeps all records from both tables. Where there is no matching record, the missing information is left empty.
| ID | Name | Score |
|---|---|---|
| 1 | Tunde | 80 |
| 2 | Amina | 90 |
| 3 | David | Missing |
| 4 | Missing | 70 |
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.
import pandas as pd
This tells Python that we want to use the Pandas library.
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.
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.
Pandas allows us to choose how two datasets should be merged. We do this using the how parameter.
result = pd.merge(
students_df,
scores_df,
on="ID",
how="inner"
)
print(result)
result = pd.merge(
students_df,
scores_df,
on="ID",
how="left"
)
print(result)
result = pd.merge(
students_df,
scores_df,
on="ID",
how="right"
)
print(result)
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.
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.
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.
| Merging | Concatenation |
|---|---|
| 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. |
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]
}
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
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.
Some records may not have matching information. Always inspect the merged dataset for missing values.
Merging connects related records using common information. Concatenation places datasets together along rows or columns.
If a key appears more than once in either dataset, merging may produce more rows than expected. Check your keys and records before merging.
| Term | Meaning |
|---|---|
| Combining Data | Bringing information from different sources together. |
| Data Merging | Joining datasets using common information. |
| Key | A column used to connect related records. |
| Inner Join | Keeps only matching records. |
| Left Join | Keeps all records from the left dataset. |
| Right Join | Keeps all records from the right dataset. |
| Outer Join | Keeps records from both datasets. |
| Concatenation | Placing datasets together along rows or columns. |
| Pandas | A Python library for working with data. |
| DataFrame | A table-like data structure in Pandas. |
In this module, you learned how to combine information from different datasets.
You learned how to:
Answer all 10 questions to check what you have learned.
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.