Last checked
Load the sales CSV with read_csv, fix names and missing prices, add a revenue column, then use groupby to total by product and by day. Sort the product totals to see top sellers and use nsmallest on daily totals to find slow days. All steps are below on made-up rows.
McTaba runs coding and data bootcamps in Kenya. This guide is written to be useful whether or not you ever join one.
Imagine a kiosk near a Nairobi bus stage. The owner types each day's sales into a phone sheet: date, product, quantity, price. After a month she has a file with typos and a few blanks. That is the file we clean here, as one step on the data science career map.
These rows are made up for the tutorial. They are not real shop figures. Install pandas with pip install pandas. If Python is new to you, start with Python for data science for beginners.
import io
import pandas as pd
csv = """date,product,qty,unit_price
2026-10-01,Unga 2kg,12,190
2026-10-01,Milk 500ml,30,65
2026-10-01,Bread,20,
2026-10-02,unga 2kg,8,190
2026-10-02,Milk 500ml,25,65
2026-10-02,Sugar 1kg,10,170
2026-10-03,Bread,18,60
2026-10-03,Milk 500ml,2,65
2026-10-04,Unga 2kg,15,190
2026-10-04,Sugar 1kg,9,170
2026-10-04,Bread,22,60
2026-10-05,Milk 500ml,28,65
"""
df = pd.read_csv(io.StringIO(csv), parse_dates=["date"])
df.info()
With your own file, replace io.StringIO(csv) with "sales.csv". The info() call lists each column, its type and how many cells are filled. Here unit_price shows 11 of 12: one price is missing.
| Method | Question it answers |
|---|---|
read_csv | How do I get the file in? |
info, isna | What is missing or the wrong type? |
str.strip, str.capitalize | Why is "unga 2kg" a separate product? |
groupby | How much per product or per day? |
nsmallest, sort_values | Which are the best and worst? |
Our Data Science to AI Engineering programme teaches pandas and data cleaning in phase 2, with weekly projects on Kenyan data.
df["product"] = df["product"].str.strip().str.capitalize()
# fill a missing price with that product's usual price
df["unit_price"] = df.groupby("product")["unit_price"].transform(
lambda s: s.fillna(s.median())
)
df["revenue"] = df["qty"] * df["unit_price"]
Two fixes happen here. Capitalising merges "unga 2kg" and "Unga 2kg" into one product. Then the blank bread price takes the median bread price, which is 60.
Filling a blank is a choice, not a fact. Ask the shop owner what happened that day. If you cannot ask, say in your notes that one price was filled.
Get weekly developer tips
Join 25,000+ developers. Practical guides, job tips, and new content — straight to your inbox.
No spam. Unsubscribe anytime.
by_product = (df.groupby("product")[["qty", "revenue"]]
.sum().sort_values("revenue", ascending=False))
print(by_product)
daily = df.groupby("date")["revenue"].sum()
print(daily.nsmallest(2))
The first table ranks products. On these rows, unga leads on shillings while milk leads on units. Those two lists answer different questions: what earns the most, and what the fridge must never run out of.
The second call prints the two slowest days. On this sample they are 3 and 5 October. A slow day is a prompt for a question: was it rain, a stock-out, or a day with no entry at all?
by_weekday = daily.groupby(daily.index.day_name()).mean()
print(by_weekday.round())
grid = df.pivot_table(index="date", columns="product",
values="qty", aggfunc="sum", fill_value=0)
print(grid)
Averaging by weekday needs weeks of data, not five days, before you trust it. The pivot table is the shopkeeper's view: one row per day, one column per product, a zero where nothing sold. A zero for milk on 4 October on this sample would be a stock-out worth asking about.
Run the same code on a real month from a shop you know, with permission. Then try three things: plot daily with daily.plot(), save the cleaned table with df.to_csv("clean.csv"), and write two sentences a shop owner could act on.
Pandas also reads your own money records. Analyse your M-Pesa statement in Python uses the same cleaning steps on a PDF. To turn daily totals into a stock plan, go to forecasting a shop's stock from till data. If you prefer asking the data in SQL, see SQL for data analysis.
Want a classroom, mentors and a capstone? Read about our Data Science to AI Engineering programme: next intake 11 January 2027, KES 140,000.
KES 140,000
Data Science to AI Engineering: 5 months full time or 8 months part time, hybrid in Nairobi. Fraud, credit and M-Pesa data from Week 1, and every model deployed. Starts 11 January 2027.
See the Data Science programme