Most pandas tutorials start with the data model and work outwards. That is the right way to learn the library and the wrong way to get useful this afternoon. This guide goes the other way: here are the operations that show up in real work, in the order you tend to need them.
Load it and look at it first
Every session starts the same way. Read the file, check the shape, look at the first few rows, and check what the types actually are. That last step catches more bugs than any other habit in this guide, because a column of numbers stored as text will silently ruin every sum you write afterwards.
import pandas as pd
df = pd.read_csv("sales.csv")
df.shape # (rows, columns)
df.head(5) # first five rows
df.dtypes # what pandas thinks each column is
df.describe() # count, mean, min, max for numeric columns
If a column that should be numeric shows up as object, something in it is not a number. Currency symbols, thousands separators and stray spaces are the usual culprits.
Selecting rows and columns
Two things confuse beginners here. Square brackets select columns, and .loc selects by label while .iloc selects by position. Learn the difference once and a whole category of errors disappears.
df["region"] # one column
df[["region", "revenue"]] # several columns
df.loc[df["revenue"] > 10000] # rows matching a condition
df.iloc[0:5] # first five rows by position
df.loc[df["region"] == "North", "revenue"] # rows and a column
Python's and and or do not work on columns. Use & and |, and wrap each condition in its own brackets: df[(df.a > 1) & (df.b < 5)].
Cleaning what you loaded
Real files are messy. These four operations handle most of it, and they are the ones an interviewer will ask you to demonstrate.
| Operation | What it does |
|---|---|
| df.isna().sum() | Counts missing values per column, so you know the size of the problem before deciding what to do about it. |
| df.fillna(0) | Replaces missing values. Pick the replacement deliberately: zero for counts, a median for measurements, a label for categories. |
| df.drop_duplicates() | Removes repeated rows. Pass subset to define what counts as a duplicate. |
| df["col"].astype(float) | Forces a type. Strip currency symbols and commas first or it raises. |
Group, aggregate, sort
This is the heart of daily analysis, and it maps almost one to one onto a SQL GROUP BY. Split the rows into groups, calculate something per group, then order the result so the answer is readable.
(df
.groupby("region")
.agg(revenue=("revenue", "sum"),
orders=("order_id", "count"))
.sort_values("revenue", ascending=False)
.reset_index())
The reset_index at the end turns the group key back into an ordinary column. Without it, the grouping column becomes the index and every later step has to work around that.
Joining two tables
Merging in pandas follows the same four shapes as a SQL join, and it takes the same care. Check your row count before and after: an unexpected jump means the key is not unique on one side, and you have quietly multiplied your data.
combined = df.merge(regions, on="region_id", how="left")
len(df), len(combined) # these should usually match for a left join
Key takeaways
- Check dtypes before you trust a single calculation.
- Use .loc for labels and .iloc for positions, and stop guessing.
- Count missing values before deciding how to fill them.
- After any merge, compare row counts to catch a duplicated key.
Turn this into a career, not a bookmark.
Data Science & Analytics with AI takes you from your first pandas import to a portfolio employers read.
Explore the course