Pandas Tutorial for Beginners: Learn pandas Step by Step
A hands-on pandas tutorial for Python beginners: load data, filter rows, clean messy columns, group and summarise, merge tables and save results, with code you can run as you read.
On this page
pandas is the reason most people who learn Python for data never go back to spreadsheets for big jobs. It loads a CSV in one line, lets you filter a million rows faster than Excel can scroll, and answers questions like "average order value by country" in a single expression.
This tutorial gets you to that point. You'll need basic Python (variables, lists, functions) and about an hour. Every example uses one small dataset, so you can type along and see each result change.
Set up in two minutes
If you don't have Python installed, the fastest start is a free online notebook: Google Colab or Kaggle Notebooks both come with pandas already installed. Locally, install it with pip:
pip install pandas
Then, at the top of every notebook or script:
import pandas as pd
The pd alias is a convention. You'll see it in every tutorial, answer and job interview, so use it too.
The two things pandas gives you
Almost everything in pandas is one of two objects:
- A Series is a single column: values plus an index (row labels).
- A DataFrame is a table: several Series that share the same index.
Create a small DataFrame to practise on. Paste this in and run it:
orders = pd.DataFrame({
"order_id": [101, 102, 103, 104, 105, 106],
"country": ["India", "UK", "India", "US", "UK", "India"],
"product": ["Course", "Book", "Course", "Course", "Course", "Book"],
"price": [499, 250, 499, 799, None, 250],
"date": ["2026-01-03", "2026-01-03", "2026-01-04", "2026-01-05", "2026-01-05", "2026-01-06"],
})
orders
Notice the None in the price column. Real data always has gaps, and you'll deal with that one in a minute.
Load real data from a file
In practice you'll read data rather than type it. The readers all start with read_:
df = pd.read_csv("orders.csv") # comma-separated files
df = pd.read_excel("orders.xlsx") # needs: pip install openpyxl
df = pd.read_json("orders.json")
Two arguments save a lot of trouble later: parse_dates=["date"] turns text dates into real dates, and usecols=[...] loads only the columns you need from a wide file.
Look before you touch anything
The first thing experienced analysts do with a new table is inspect it. These four calls tell you most of what you need:
orders.head() # first 5 rows
orders.shape # (rows, columns) → (6, 5)
orders.info() # column types and missing values
orders.describe() # count, mean, min, max for numeric columns
info() is the one beginners skip and regret. It would show you that price has 5 non-null values out of 6, and that date is stored as text (object), not as a date.
Select columns and rows
One column returns a Series; a list of columns returns a DataFrame:
orders["price"]
orders[["country", "price"]]
For rows, pandas has two tools, and the difference matters:
.locselects by label:orders.loc[0, "country"].ilocselects by position:orders.iloc[0, 1]
With the default numeric index they look identical. After you sort or filter, they don't, and mixing them up is the most common source of "why is this the wrong row?"
Filter rows with conditions
Filtering is where pandas starts to feel fast. Put a condition inside square brackets:
orders[orders["country"] == "India"]
orders[orders["price"] > 300]
orders[(orders["country"] == "India") & (orders["product"] == "Course")]
orders[orders["country"].isin(["UK", "US"])]
Two rules trip people up: use & and | instead of and and or, and wrap each condition in parentheses. If you forget, pandas raises an error that looks far scarier than the mistake.
Clean the data
Fix the types first. Dates stored as text can't be sorted or grouped by month properly:
orders["date"] = pd.to_datetime(orders["date"])
Then decide what to do about missing values. There's no universal answer; it depends on what the gap means.
orders["price"].isna().sum() # how many are missing? → 1
orders.dropna(subset=["price"]) # drop rows without a price
orders["price"].fillna(orders["price"].median()) # or fill with the median
Most methods return a new DataFrame instead of changing the original. If you want to keep the result, assign it: orders = orders.dropna(subset=["price"]). Forgetting this is the second most common beginner bug.
A few more cleaning moves you'll use constantly:
orders = orders.rename(columns={"price": "price_inr"})
orders = orders.drop_duplicates()
orders["country"] = orders["country"].str.strip().str.title()
Add new columns
Calculations work on whole columns at once, with no loops:
orders["price_usd"] = orders["price_inr"] / 84
orders["month"] = orders["date"].dt.month
orders["is_course"] = orders["product"] == "Course"
If you catch yourself writing a for loop over rows, stop and look for a column-wise way to do it. It's usually shorter and hundreds of times faster.
Group and summarise
This is the part that answers business questions. groupby splits the table, applies a calculation and combines the results:
orders.groupby("country")["price_inr"].sum()
orders.groupby("country").agg(
orders=("order_id", "count"),
revenue=("price_inr", "sum"),
avg_price=("price_inr", "mean"),
)
The second form, with named aggregations, gives you clean column names and is what you should reach for in real work. Add .sort_values("revenue", ascending=False) to rank the result.
For a spreadsheet-style cross-tab, use a pivot table:
pd.pivot_table(orders, index="country", columns="product",
values="price_inr", aggfunc="sum", fill_value=0)
Combine two tables
Real analysis almost always joins data from two sources. If you know SQL, merge is a JOIN:
countries = pd.DataFrame({
"country": ["India", "UK", "US"],
"region": ["Asia", "Europe", "Americas"],
})
orders.merge(countries, on="country", how="left")
Check the row count after every merge. If it grows unexpectedly, one of your keys has duplicates. That habit alone will save you from publishing wrong numbers. Our guide to SQL practice sites covers the same join logic from the database side.
Save your results
summary = orders.groupby("country")["price_inr"].sum().reset_index()
summary.to_csv("revenue_by_country.csv", index=False)
summary.to_excel("revenue_by_country.xlsx", index=False)
index=False stops pandas writing its row numbers as an extra column, which is almost never what the person opening the file wants.
Practise on real data next
Tutorials make pandas look tidy. Real files aren't. Pick a public dataset you actually care about (football results, your city's open data, a Kaggle dataset) and answer three questions you wrote down before looking at the data. You'll hit encoding errors, odd date formats and duplicate keys, and that's exactly the practice that matters.
Good next resources, in order:
- The official pandas getting-started tutorials, short and task-based, free.
- Data Analysis with Python from IBM, if you want a structured course with graded labs.
- Foundations of Data Analysis with Pandas and Python, a slower, more guided option.
If Python itself still feels shaky, spend a week on CS50's Python course or Python for Everybody first. pandas is much easier once lists, dictionaries and functions are second nature. For the bigger picture of where pandas fits in a data career, see our data analyst roadmap, and compare options in Python courses and data analysis courses.
Common questions
How long does it take to learn pandas?
The basics in this tutorial take a few hours to learn and a couple of weeks of regular use to remember. Being genuinely quick with groupby, merges and dates usually takes one or two months of working on real datasets.
Should I learn pandas or SQL first?
If you're aiming for data analyst jobs, start with SQL; it shows up in more job adverts and interviews. If you already know some Python, learning pandas first is fine. You'll end up needing both, and the concepts transfer directly.
Is pandas still worth learning with newer libraries around?
Yes. Faster libraries such as Polars are growing, but pandas is still what most tutorials, jobs and existing code use. Its ideas (DataFrames, grouping, joins) carry over to every alternative.
Keep reading
freeCodeCamp Certifications Explained: Free, but Worth It?
How freeCodeCamp certifications work, what changed with the legacy V8 versions, which one to start with, how long the JavaScript certification takes, and what employers make of them.
Is There a Svelte Certification? How to Learn Svelte Free
There's no official Svelte certification. Here's what the certificates you'll find actually are, a free path through Svelte 5 and SvelteKit, and the paid courses worth considering.
How to Learn Angular for Free: A Practical Path
A free, step-by-step path to modern Angular: what to know first, the official tutorial, signals and standalone components, projects to build, and when a paid course is worth it.