Cleaning data
Real data is messy. Find the gaps, fill them, and fix the types.
Run this room's code on your own computer in VS Code. You set this up in Module 07.
Learn it
Find missing values with isna(), then fill or drop them.
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.
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)
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
Try it yourself
+10 XPInstead 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.
Capture the flag
+10 XPWhich 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.
Cleaning report
+20 XPWrite 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.