Introduction
If you work with data in Python — spreadsheets, CSV files, API responses, database dumps — pandas is the tool you'll reach for every single day. It turns messy rows and columns into something you can filter, group, and analyze in just a few lines.
Pandas is built on top of NumPy and powers most data science and analytics work in Python. Once it clicks, tasks that took dozens of lines of loops become one-liners.
This guide assumes zero pandas knowledge. By the end you'll understand the core data structures, know how to load and clean real data, and have copy-paste snippets for the operations you'll actually use.
Installing Pandas
pip install pandas
Then import it. The universal convention is to alias it as pd — every tutorial and codebase does this:
import pandas as pd
The Two Core Structures
Pandas has exactly two data structures you need to know. Everything else builds on them.
| Structure | What it is |
|---|---|
| Series | A single column — a 1D labeled array |
| DataFrame | A full table — rows and columns, like a spreadsheet |
Series — a single column
import pandas as pd
prices = pd.Series([10, 25, 30, 15])
print(prices)
0 10
1 25
2 30
3 15
dtype: int64
That left-hand column (0, 1, 2, 3) is the index — pandas labels every value automatically.
DataFrame — the whole table
A DataFrame is just a collection of Series sharing the same index. This is what you'll use 95% of the time:
data = {
"name": ["Ali", "Sara", "John", "Mia"],
"age": [25, 30, 35, 28],
"city": ["Lahore", "Karachi", "London", "Dubai"],
}
df = pd.DataFrame(data)
print(df)
name age city
0 Ali 25 Lahore
1 Sara 30 Karachi
2 John 35 London
3 Mia 28 Dubai
Reading Data from a File
In real life you rarely type data by hand — you load it. Pandas reads almost any format:
df = pd.read_csv("data.csv") # CSV files
df = pd.read_excel("data.xlsx") # Excel
df = pd.read_json("data.json") # JSON
read_csv is the one you'll use most. It handles headers, data types, and millions of rows effortlessly.
Inspecting Your Data
The first thing you do with any new dataset is look at it. These five methods are your starting routine:
df.head() # first 5 rows
df.tail(3) # last 3 rows
df.shape # (rows, columns) → e.g. (4, 3)
df.info() # column names, types, non-null counts
df.describe() # quick stats: mean, min, max, etc.
| Method | What it tells you |
|---|---|
head() |
A quick peek at the top rows |
shape |
How big the dataset is |
info() |
Data types and missing values |
describe() |
Summary statistics for numeric columns |
Selecting Columns
Grab a single column (returns a Series):
df["name"]
Grab multiple columns (returns a DataFrame) — note the double brackets:
df[["name", "city"]]
Selecting Rows — loc and iloc
This is where beginners get confused, so here's the simple rule:
locselects by label (the index name)ilocselects by integer position (like a Python list)
df.loc[0] # row with index label 0
df.iloc[0] # first row by position
df.loc[0:2] # rows 0,1,2 by label (inclusive!)
df.iloc[0:2] # rows 0,1 by position (exclusive, like slicing)
# A specific cell: row label 1, column "name"
df.loc[1, "name"] # → "Sara"
Filtering Rows (the most important skill)
Filtering is what makes pandas powerful. You write a condition, and pandas keeps only the rows where it's True:
# People older than 28
df[df["age"] > 28]
name age city
1 Sara 30 Karachi
2 John 35 London
Combine conditions with & (and) / | (or) — wrap each condition in parentheses:
# Older than 25 AND living in London
df[(df["age"] > 25) & (df["city"] == "London")]
# In Lahore OR Dubai
df[df["city"].isin(["Lahore", "Dubai"])]
Common mistake: using Python's
and/orinstead of&/|. Pandas needs the symbols, and each condition must be in parentheses or you'll get an error.
Adding and Modifying Columns
Create a new column by assigning to it:
# Add a column derived from existing data
df["age_in_5_years"] = df["age"] + 5
# Add a flag column based on a condition
df["is_adult"] = df["age"] >= 18
Handling Missing Data
Real data is messy — missing values show up as NaN (Not a Number). Pandas gives you clean tools to deal with them:
df.isnull().sum() # count missing values per column
df.dropna() # drop rows with any missing values
df.fillna(0) # replace missing values with 0
df["age"].fillna(df["age"].mean()) # fill with the column average
| Method | When to use it |
|---|---|
dropna() |
When missing rows are few and safe to remove |
fillna(value) |
When you want a sensible default |
fillna(mean/median) |
For numeric columns where you can't lose rows |
Sorting
df.sort_values("age") # ascending
df.sort_values("age", ascending=False) # descending
df.sort_values(["city", "age"]) # sort by multiple columns
Grouping and Aggregating
groupby is pandas' superpower — it's the equivalent of a pivot table or SQL GROUP BY. Split your data into groups, then compute something for each:
# Average age per city
df.groupby("city")["age"].mean()
# Count of people per city
df.groupby("city").size()
# Multiple stats at once
df.groupby("city")["age"].agg(["mean", "min", "max", "count"])
mean min max count
city
Dubai 28 28 28 1
Karachi 30 30 30 1
Lahore 25 25 25 1
London 35 35 35 1
This single concept powers most real analysis: "average sales per region," "total revenue per month," "number of orders per customer."
A Quick Real Example
Putting it together — imagine a sales CSV. Here's a typical mini-analysis:
import pandas as pd
# 1. Load
df = pd.read_csv("sales.csv")
# 2. Clean — drop rows with no amount
df = df.dropna(subset=["amount"])
# 3. Filter — only completed orders
completed = df[df["status"] == "completed"]
# 4. Analyze — total revenue per region
revenue = completed.groupby("region")["amount"].sum()
# 5. Sort — biggest regions first
revenue = revenue.sort_values(ascending=False)
print(revenue)
Five steps — load, clean, filter, group, sort — covers a huge share of real-world data work.
Exporting Your Results
Once you've crunched the numbers, save them back out:
df.to_csv("output.csv", index=False) # index=False skips the row numbers
df.to_excel("output.xlsx", index=False)
df.to_json("output.json")
Pandas Cheat Sheet
LOAD
pd.read_csv("file.csv") → load a CSV into a DataFrame
INSPECT
df.head() / df.info() / df.describe()
SELECT
df["col"] → one column
df[["a", "b"]] → multiple columns
df.loc[label] / df.iloc[pos] → rows by label / position
FILTER
df[df["age"] > 30] → rows matching a condition
df[(cond1) & (cond2)] → combine with & and |
CLEAN
df.dropna() / df.fillna(0) → handle missing data
ANALYZE
df.sort_values("col") → sort
df.groupby("col").mean() → group + aggregate
SAVE
df.to_csv("out.csv", index=False)
Final Thought
Pandas feels overwhelming at first because it can do so much — but you only need a handful of operations to be productive: load, inspect, select, filter, group, and save. Master those six and you can handle most data tasks that come your way.
The best way to learn is to grab a real CSV — a bank statement, a spreadsheet export, anything — load it with read_csv, and start poking at it. Print things, filter things, group things. Nothing teaches pandas faster than wrangling data you actually care about.