Finding missing values
Spot blanks with isna and decide when to drop rows.
Real data has gaps: a sensor skipped a reading, someone left a form field blank. pandas marks a missing value as NaN ("not a number"). df.isna() gives True wherever a value is missing, and df.isna().sum() counts the gaps in each column. Summaries like mean() quietly skip NaN, which is usually what you want, but you should always know how many values are missing. dropna() removes rows with any gap; dropna(subset=["steps"]) only looks at the columns you name.
import io
import pandas as pd
csv_text = """name,age,steps,sleep
Ana,34,8200,7.5
Bo,51,,6.0
Cy,28,10400,
Dee,45,6100,8.0
Eli,,7300,6.5
Fay,39,,7.0
"""
health = pd.read_csv(io.StringIO(csv_text))
print(health)
print(health.isna().sum())
print(len(health.dropna()), "complete rows")
print(health["steps"].mean())The blank cells in the CSV became NaN. steps has 2 missing values, sleep and age have 1 each. Only 2 rows are complete. mean() averages the 4 steps values that exist.
You cannot find NaN with ==. NaN == NaN is False by design. Always use isna() or notna().
Dropping rows throws away information. If many rows have one small gap, think about filling instead (next lesson).