A DataFrame is a table in Python Watch
pandas stores data as a DataFrame: rows and named columns, like a spreadsheet you control with code. pandas comes ready in Colab. By custom, it is imported with the short name pd.
import pandas as pd
sales = pd.DataFrame({
"day": ["Mon", "Mon", "Tue", "Tue", "Wed", "Wed"],
"item": ["Tea", "Samosa", "Tea", "Samosa", "Tea", "Samosa"],
"units": [120, 45, 135, None, 150, 60],
"price": [15, 20, 15, 20, 15, 20],
})
print(sales.head(3))
print(sales.shape)
# Output:
# day item units price
# 0 Mon Tea 120.0 15
# 1 Mon Samosa 45.0 20
# 2 Tue Tea 135.0 15
# (6, 4)
head(3) shows the first three rows; shape gives (rows, columns). With real data you would load a file instead, for example pd.read_csv("sales.csv").
First, look for problems Watch
Real data always has gaps and mistakes. Professionals check before they trust it. None above means Tuesday's samosa count is missing - pandas shows it as NaN, "not a number".
print(sales["units"].isna().sum(), "missing value")
# Output:
# 1 missing value
- isna().sum() counts missing values in a column.
- describe() gives the count, average, minimum and maximum of every number column - great for spotting a typo like 15000 cups of tea.
Clean the data carefully Watch
A common way to fill a gap is the median, the middle value. But think first: the median of the whole column mixes tea and samosas, and would claim we sold 120 samosas on Tuesday. The careful way fills the gap with the median of the same item.
# fill the gap with the median of the SAME item (a samosa count, not a tea count)
sales["units"] = sales["units"].fillna(sales.groupby("item")["units"].transform("median"))
sales["revenue"] = sales["units"] * sales["price"]
print(sales[sales["item"] == "Samosa"])
# Output:
# day item units price revenue
# 1 Mon Samosa 45.0 20 900.0
# 3 Tue Samosa 52.5 20 1050.0
# 5 Wed Samosa 60.0 20 1200.0
The new revenue column is units times price, worked out for every row at once - no loop needed. The line sales[sales["item"] == "Samosa"] keeps only the rows where the condition is true. This is called filtering.
Summarise with groupby Watch
groupby splits the table into groups and summarises each one - the code version of a pivot table.
print(sales.groupby("item")["revenue"].sum())
# Output:
# item
# Samosa 3150.0
# Tea 6075.0
# Name: revenue, dtype: float64
Lesson learned Watch
pandas holds data as DataFrames. Look before you trust: check shape and missing values, clean with care and write down why, then filter and summarise with groupby. These moves come first in almost every AI project.
Frequently asked questions
What is pandas in Python?
pandas is a free Python library for working with tables of data: loading files, cleaning, filtering and summarising them.
How do I handle missing values in pandas?
Find them with isna().sum(), then remove the rows with dropna() or fill them with fillna() - choosing a fill value that makes sense for that group, and noting your choice.
What does groupby do in pandas?
It splits a table into groups by a column, such as item or city, and summarises each group, for example with sum() or mean().
Last checked: November 2026. Tools and free tiers change - check each official page. All examples are original; the data in them is made up for teaching.