0
0
Data Analysis Pythondata~5 mins

Inner join in Data Analysis Python

Choose your learning style9 modes available
Introduction

An inner join helps you combine two tables by matching rows with the same values in both tables. It shows only the matching data.

You want to see customers and their orders, but only those who have placed orders.
You have a list of students and a list of their test scores, and you want to combine them where students have scores.
You want to find employees and their departments, but only for employees assigned to a department.
Syntax
Data Analysis Python
pd.merge(left_table, right_table, how='inner', on='common_column')
Use how='inner' to specify an inner join.
The on parameter is the column name(s) to match in both tables.
Examples
Joins two dataframes on the 'id' column, keeping only rows with matching 'id' in both.
Data Analysis Python
pd.merge(df1, df2, how='inner', on='id')
Combines customers and orders where customer IDs match.
Data Analysis Python
pd.merge(customers, orders, how='inner', on='customer_id')
Sample Program

This example joins the students and scores tables on the 'student_id' column. It shows only students who have scores.

Data Analysis Python
import pandas as pd

# Create first table
students = pd.DataFrame({
    'student_id': [1, 2, 3, 4],
    'name': ['Alice', 'Bob', 'Charlie', 'David']
})

# Create second table
scores = pd.DataFrame({
    'student_id': [2, 4, 5],
    'score': [85, 92, 88]
})

# Inner join on 'student_id'
result = pd.merge(students, scores, how='inner', on='student_id')
print(result)
OutputSuccess
Important Notes

Inner join keeps only rows with matching keys in both tables.

If no rows match, the result is an empty table.

Summary

Inner join combines tables by matching rows on a key.

Only rows with matching keys in both tables appear in the result.

Use pd.merge() with how='inner' in pandas.