Local lab20 min+60 XP

Cleaning data

Real data is messy. Find the gaps, fill them, and fix the types.

0% done

Run this room's code on your own computer in VS Code. You set this up in Module 07.

TASK 1

Learn it

Key idea

Find missing values with isna(), then fill or drop them.

df["coins"]2509061040480150NaN75after cleaning2509061040480150075isna().sum() found 1 missing value.fillna(0)
The missing coins value is found and filled with 0.

Data scientists say most of their time goes on cleaning. First find the problems: df.isna().sum() counts the missing values in each column.

Then decide what to do: dropna() removes rows with missing values, and fillna(value) fills them in, for example with 0 or the column's mean. There is no single right answer: think about what the data means.

Fix types too. The escaped column holds the text "yes" or "no". Turning it into True and False with df["escaped"] == "yes" makes it much easier to count.

Real life linkSurvey answers come back messy: blanks, "N/A", ages typed as words. Cleaning comes first, and it is often most of the work.
Watch outNever quietly change data without saying so. When you report results, mention how you cleaned the data, because it changes the answers.
Example code
import pandas as pd

df = pd.read_csv("players.csv")
print(df.isna().sum())
df["coins"] = df["coins"].fillna(0)
df["minutes"] = df["minutes"].fillna(df["minutes"].mean()).round(1)
df["escaped"] = df["escaped"] == "yes"
print(df)
Output on your computer
player     0
level      0
coins      1
minutes    1
escaped    0
dtype: int64
  player  level  coins  minutes  escaped
0   Nova      7  250.0     12.5     True
1    Kai      4   90.0     20.0    False
2   Mira      9  610.0      8.2     True
3    Leo      2   40.0     13.6    False
4    Ivy      8  480.0      9.7     True
5    Zed      5  150.0     15.3     True
6    Ava      6    0.0     11.0    False
7    Ben      3   75.0     18.4    False
TASK 2

Try it yourself

+10 XP

Instead of filling in the gaps, drop the rows with any missing value and print how many rows are left.

Create a free account to start this lab

Sign in to write, run and check your code here. Your progress, XP and code are saved to your account. No card needed.

TASK 3

Capture the flag

+10 XP

Which DataFrame method fills in missing values?

Create a free account to start this lab

Sign in to answer the flag question. Your progress, XP and code are saved to your account. No card needed.

HOMEWORK

Cleaning report

+20 XP

Write a script that loads the data, prints the number of missing values per column, fills missing coins with 0 and missing minutes with the median, converts escaped to True/False, and saves the result as players_clean.csv with df.to_csv("players_clean.csv", index=False).

Create a free account to start this lab

Sign in to do this homework and have it checked. Your progress, XP and code are saved to your account. No card needed.