Real data is never as tidy as py-sales.csv. People type branch names in different ways, systems export prices with currency signs, and some cells are left empty. Analysts often say that 80% of the job is cleaning. That's a slight exaggeration, but only slight.
py-sales-raw.csv is the same six months of sales, exported the way a real system might export it. Download it and load it:
import pandas as pd
raw = pd.read_csv("py-sales-raw.csv")
Spot the problems first
raw.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 918 entries, 0 to 917
Data columns (total 11 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 order_id 918 non-null object
1 date 918 non-null object
2 branch 918 non-null object
3 rep 912 non-null object
4 customer 918 non-null object
5 product 918 non-null object
6 category 918 non-null object
7 qty 910 non-null float64
8 unit_price 918 non-null object
9 payment 918 non-null object
10 status 918 non-null object
dtypes: float64(1), object(10)
memory usage: 79.0+ KB
Two clues already. unit_price is object (text), but prices should be numbers. And rep and qty have fewer non-null values than the 918 rows, so some values are missing. You'll deal with missing values in the next lesson.
Now look at the branch names:
raw["branch"].value_counts()
branch
Kano 203
Abuja 150
Port Harcourt 145
Ibadan 107
Lekki 97
Ikeja 79
kano 10
PORT HARCOURT 10
Kano 9
KANO 9
kano 9
IBADAN 6
port harcourt 6
ABUJA 6
port harcourt 5
abuja 5
Abuja 5
Kano 5
ibadan 4
ikeja 4
Ibadan 3
lekki 3
LEKKI 3
Lekki 3
Ikeja 3
ikeja 3
Lekki 3
Abuja 3
Port Harcourt 3
IKEJA 3
lekki 3
abuja 3
Ibadan 2
Ikeja 2
ibadan 2
Port Harcourt 2
Name: count, dtype: int64
There should be six branches. Instead, "Kano", "kano", "KANO" and " Kano" are all counted separately, so a groupby would split Kano's sales across several rows.

Tidy the text
The .str accessor applies text methods to every value in a column at once:
raw["branch"] = raw["branch"].str.strip().str.title()
raw["branch"].value_counts()
branch
Kano 245
Abuja 172
Port Harcourt 171
Ibadan 124
Lekki 112
Ikeja 94
Name: count, dtype: int64
Six branches. .strip() removed the extra spaces, and .title() made the capitals consistent. Other useful .str methods: .lower(), .upper(), .replace("old", "new"), .contains("text") and .startswith("text").
Turn text prices into numbers
Look at some prices:
raw["unit_price"].head(8).tolist()
['59800', '79600', '10000', '17800', '53000', '₦10,000', '₦62,200', '₦12,600']
Some have a ₦ sign and commas. Remove both, then convert with pd.to_numeric():
raw["unit_price"] = (
raw["unit_price"]
.astype(str)
.str.replace("₦", "")
.str.replace(",", "")
)
raw["unit_price"] = pd.to_numeric(raw["unit_price"])
raw["unit_price"].dtype
dtype('int64')
int64: real numbers now, so you can add, average and multiply them. .astype(str) first makes sure every value is text, so the .str methods work even on values that were already numbers.
Check your work
After every cleaning step, check it did what you expected:
raw["unit_price"].describe().round(0)
count 918.0
mean 26873.0
std 22846.0
min 2400.0
25% 12600.0
50% 17400.0
75% 38400.0
max 81900.0
Name: unit_price, dtype: float64
The smallest price is ₦2,400 and the largest ₦81,900, the same as in the clean file. If you'd seen a maximum of ₦8,190,000, you'd know a decimal point or extra zero had crept in.
Check yourself
Answer in your head first, then open each question to see the answer and why.
A column that should hold prices shows the type object in info(). What does that suggest?
- A Some values are stored as text, so the column isn't numeric yet
- B The column is perfect
- C The column holds dates
- D The file is corrupt and can't be used
Show the answer
A) Some values are stored as text, so the column isn't numeric yet — object usually means text; prices need to be numbers.
Which line turns " kano", "KANO" and "Kano" into the same value?
- A df["branch"].str.strip().str.title()
- B df["branch"].sum()
- C df["branch"].drop()
- D df["branch"].sort_values()
Show the answer
A) df["branch"].str.strip().str.title() — strip removes spaces and title makes the capitals consistent.
What does errors="coerce" do in pd.to_numeric()?
- A Turns values that can't be converted into missing values instead of raising an error
- B Deletes the whole column
- C Rounds every number
- D Converts numbers into text
Show the answer
A) Turns values that can't be converted into missing values instead of raising an error — Then you can find and inspect the problem values.
More free lessons: What Python can do for your work · Lists and loops · Loading data and taking a first look · Combining many files into one · Using AI coding assistants wisely