Real tables have holes. Some customers never gave a city or an age, so those cells in customers.csv are empty. pandas reads an empty cell as NaN ("not a number"), its mark for a missing value. Before you compute anything, find the holes and decide what to do with each column.
isna() gives back a table of the same shape, True where a cell is empty and False elsewhere; .sum() on it counts the empty cells in each column. dropna() gives back the table without every row that has an empty cell in any column; with subset=[...] it only looks at the columns you list. median() gives back the middle value of a column, skipping the empty cells, and fillna(value) gives back the column with every empty cell set to value. Here they are on a small made-up column, built from a list with pd.Series:
scores = pd.Series([8, None, 4, 6])
print(scores.isna().sum()) # 1
print(len(scores.dropna())) # 3
scores = scores.fillna(scores.median()) # the gap becomes 6.0
None of these change the table by themselves: they give back a new one, which you count or store. And dropping is not always the answer: a row with no age still has good visits, orders and spent, so for age you fill the holes with a typical value instead. You choose, column by column.
Your program must print:
customer_id 0
city 9
age 14
joined 0
newsletter 0
visits 0
orders 0
spent 0
days_since_last 0
returned_item 0
bought_again 0
Rows without any gap: 279
Rows with a city: 291
Median age: 37.0 missing now: 0
Your task: print the number of empty cells in each column, the number of rows left after dropna() and after dropping only the rows with no city, then fill the empty ages with the median age and print it with how many ages are still empty.
Assignment
Follow the Coding Tutorial to count, drop and fill the missing values.
Hintdropna() gives back a new, shorter table: count its rows with len(...). fillna gives back a new column too, so store it back in df["age"].
Free plan: 30 tutor messages. PRO is unlimited.
