Pandas · Handle missing values
Google Search Analytics
Course overviewGoogle Search · Clean values and create columns

Handle missing values

Identify missing values, keep known observations, and choose when filling or dropping is appropriate.

Step 1 of 3 · Learn

Find missing values

Use .isna() to identify missing values and .notna() to identify present values. Do not compare with None or NaN using ==; zero and an empty string are not missing values.

indexnamelabel
0AriNA
1Bo
2CamA
indexnamelabel
0AriNA
Only the missing label matches. The empty string is still a present value.

Drop only the rows you mean to drop

dropna(subset=[...]) removes rows missing values in the specified columns. Without subset, an unrelated missing value can remove a row you intended to keep.

indexnamelabel
0AriNA
1Bo
2CamA
indexnamelabel
1Bo
2CamA
Check only label; keep both rows where that value is present.

Fill labels without inventing facts

fillna() replaces missing values; a column-name dictionary limits which columns change. A display label can use "Unknown", but never fill anonymous user IDs with a shared ID or turn an unknown measurement into zero without justification.

indexnamelabel
0AriNA
1Bo
2CamA
indexnamelabel
0AriUnknown
1Bo
2CamA
Only the missing label changes. This is a display choice, not a recovered fact.
▷ Your turn

Return search_id, user_id, and query for searches whose user_id is missing. Keep the missing values and source row order. Save the DataFrame as result.

LANGUAGEPython · Pandas

Loading Python and Pandas…

Run the code to see DataFrame results here.