Introduction to Pandas
Learn what Pandas is, why it is important in Python, and how Series and DataFrames help you organize, explore, clean, and analyze structured data.
1. Learning Objectives
By the end of this lesson, you will be able to:
- Define Pandas and explain its purpose.
- Understand why data analysis is important.
- Recognize the difference between Python lists and Pandas structures.
- Understand Series and DataFrames at a basic level.
- Identify common uses of Pandas in real-world projects.
- Write a simple Python program using Pandas.
2. What Is Pandas?
Pandas is an open-source Python library used for data manipulation and analysis. It provides convenient data structures and tools for working with tabular and labelled data.
With Pandas, you can load information from files, inspect rows and columns, filter records, handle missing values, calculate statistics, combine datasets, and prepare data for reports or machine learning.
A Practical Example
Imagine that a teacher has marks for several students. Instead of manually calculating the average, Pandas can store the records in a DataFrame and calculate the average marks using a single instruction.
import pandas as pd
data = {
"Student": ["Aarav", "Priya", "Rahul", "Neha"],
"Marks": [75, 88, 92, 65]
}
df = pd.DataFrame(data)
print(df)
print("Average Marks:", df["Marks"].mean())
Output:
Student Marks 0 Aarav 75 1 Priya 88 2 Rahul 92 3 Neha 65 Average Marks: 80.0
import pandas as pd imports
the Pandas library and assigns it the commonly used alias
pd.
3. Why Do We Need Pandas?
Real-world data often comes from spreadsheets, databases, CSV files, websites, and business applications. This data may contain thousands of records, missing values, duplicate rows, and inconsistent formats.
Pandas provides tools to handle these tasks in a clear and repeatable way.
Common Tasks You Can Perform
- Read data from CSV, Excel, and JSON files.
- Inspect the first and last rows of a dataset.
- Select particular columns or rows.
- Filter records using conditions.
- Find and handle missing values.
- Remove duplicate records.
- Group data and calculate summaries.
- Combine multiple datasets.
- Prepare data for visualization and reporting.
Example: Filter Students Who Passed
import pandas as pd
df = pd.DataFrame({
"Student": ["Aarav", "Priya", "Rahul", "Neha"],
"Marks": [75, 88, 92, 35]
})
passed = df[df["Marks"] >= 40]
print(passed)
Output:
Student Marks 0 Aarav 75 1 Priya 88 2 Rahul 92
The expression df["Marks"] >= 40 checks
every student's marks. Pandas returns only the rows
that satisfy the condition.
4. Understanding Series and DataFrames
Pandas mainly provides two data structures that beginners should understand first: Series and DataFrame.
Series
A Series is a one-dimensional labelled data structure. You can think of it as a single column of values with an associated index.
import pandas as pd marks = pd.Series([75, 88, 92]) print(marks)
Output:
0 75 1 88 2 92 dtype: int64
DataFrame
A DataFrame is a two-dimensional labelled data structure with rows and columns. It is useful for representing tables such as student records, sales reports, and employee information.
students = pd.DataFrame({
"Name": ["Aarav", "Priya", "Rahul"],
"Marks": [75, 88, 92]
})
print(students)
Output:
Name Marks 0 Aarav 75 1 Priya 88 2 Rahul 92
Visual 1: A Series represents one labelled dimension; a DataFrame represents a table with rows and columns.
Quick Comparison
| Feature | Series | DataFrame |
|---|---|---|
| Dimensions | One-dimensional | Two-dimensional |
| Structure | One labelled sequence | Rows and columns |
| Typical use | One set of values | Tabular datasets |
| Example | Student marks column | Student names and marks table |
5. Pandas vs Python Lists
Python lists are general-purpose containers. Pandas Series and DataFrames provide additional features designed for labelled data, table operations, filtering, and analysis.
| Feature | Python List | Pandas |
|---|---|---|
| Data structure | Sequence of items | Series and DataFrame |
| Column labels | Not built in | Available in DataFrames |
| Filtering tables | Usually requires additional logic | Convenient conditional selection |
| Missing data | Requires custom handling | Built-in missing-data tools |
| File handling | Requires other code or libraries | Convenient CSV, Excel and JSON functions |
6. Where Is Pandas Used?
Business and Sales Analysis
Analyze revenue, compare products, summarize sales by region, and prepare monthly reports.
Education
Study student marks, calculate class averages, identify missing scores, and summarize examination results.
Finance
Organize transaction records, calculate financial summaries, and analyze time-based datasets.
Data Science and Machine Learning
Inspect datasets, clean inconsistent values, transform columns, and prepare structured data for later analysis.
Reporting and Automation
Combine files, generate summaries, and automate repetitive data preparation tasks.
7. Your First Pandas Program
Let's create a small employee dataset and calculate the total and average salary. This example demonstrates how a DataFrame stores records and how Pandas calculates values from a column.
import pandas as pd
employees = pd.DataFrame({
"Name": ["Amit", "Sneha", "Rohan"],
"Salary": [30000, 45000, 40000]
})
print("Employee Data:")
print(employees)
print("Total Salary:", employees["Salary"].sum())
print("Average Salary:", employees["Salary"].mean())
Expected Output:
Employee Data:
Name Salary
0 Amit 30000
1 Sneha 45000
2 Rohan 40000
Total Salary: 115000
Average Salary: 38333.333333333336
Understanding the Code
-
import pandas as pdimports the Pandas library. -
pd.DataFrame()creates a table from the supplied data. -
The
NameandSalarykeys become column labels. -
employees["Salary"]selects the Salary column. -
.sum()calculates the total salary. -
.mean()calculates the average salary.
ModuleNotFoundError: No module named 'pandas',
Pandas is not installed in the Python environment currently
being used. We will cover installation in the next lesson.
8. Common Beginner Mistakes
Mistake 1: Forgetting to Import Pandas
You must import Pandas before using the pd
alias or calling pd.DataFrame().
Mistake 2: Misspelling Column Names
Column labels must match the names in the DataFrame.
For example, Salary and salary
are different labels.
import pandas as pd
df = pd.DataFrame({
"Salary": [30000, 45000]
})
print(df["Salary"])
Mistake 3: Assuming a DataFrame Is a Python List
A DataFrame is a table with labelled columns and an index.
You can select columns, filter rows, and use methods such
as head(), info(), and
describe() to inspect it.
9. Classroom Practical Activity
Activity: Analyze Student Marks
Create a DataFrame with the names and marks of five students. Display the table and calculate the total, average, highest, and lowest marks.
Starter Code
import pandas as pd
students = pd.DataFrame({
"Name": ["Aarav", "Priya", "Rahul", "Neha", "Karan"],
"Marks": [72, 85, 91, 64, 78]
})
print(students)
print("Total:", students["Marks"].sum())
print("Average:", students["Marks"].mean())
print("Highest:", students["Marks"].max())
print("Lowest:", students["Marks"].min())
Practice Questions
- What is Pandas used for?
- What does
import pandas as pdmean? - What is a Pandas Series?
- What is a DataFrame?
- How do you select a column from a DataFrame?
- Which method calculates the average of a numerical column?
- Name three real-world uses of Pandas.
Summary
Pandas is a Python library for data manipulation and analysis. It provides Series for one-dimensional labelled data and DataFrames for tabular data arranged in rows and columns.
In this lesson, you learned why Pandas is useful, where it is used, how to create a DataFrame, select a column, filter records, and calculate basic statistics. You are now ready to prepare your Python environment and start using Pandas.