Skip to content
tezvyn:

Describe strategies for handling missing values in pandas DataFrames

Source: pandas.pydata.orgEasyHow cards are made

Describe strategies for handling missing values in pandas DataFrames

Tests practical judgment on cleaning trade-offs. Good answers contrast dropna when data is abundant against fillna imputation to preserve rows, noting bias risk. Red flag: prescribing one fix without asking why values are missing or what the model needs.

What's really being asked

This question probes whether you treat missing data as a modeling decision rather than a code snippet. Interviewers want to see that you understand deletion loses information, imputation introduces bias, and the right choice depends on the missingness mechanism and the downstream task.

The full answer

First, deletion strategies such as dropna on rows or columns, which are appropriate when data is missing completely at random and the remaining sample is still large enough to power the analysis, but which risk discarding useful signal if missingness is correlated with the target. Second, single-value imputation using fillna with the mean or median for numerical columns, which preserves row count and is fast, but artificially reduces variance and ignores inter-column relationships; or forward fill and backward fill for time-ordered data, which respects sequence but can propagate stale values. Third, model-based imputation such as KNN or iterative imputers, which captures correlations but is computationally expensive and can leak target information if done carelessly. Fourth, the importance of inspecting missingness patterns with isna and notna before acting, and the fact that pandas uses different sentinels such as np.nan for floats, NaT for datetimes, and pd.NA for nullable dtypes like Int64 or boolean, which affect how operations propagate.

The mistakes people make

Prescribing mean imputation for every column without checking distributions or missingness rates. Filling NaNs with zeros as a default, which creates misleading values for features like income or temperature. Dropping all rows containing any NaN without considering how much data is lost. Ignoring that np.nan does not equal itself, so equality checks fail and you must use isna instead. Overlooking that nullable extension dtypes such as Int64 preserve integer type whereas standard numpy int64 coerces missing values to float.

What usually comes next

How would you handle missingness in a time-series index versus a cross-sectional table? What would you do if a single column is ninety percent missing? How does imputation change the variance of a feature and why does that matter for a linear model? When would you use a mask or indicator column instead of filling the value?

A concrete example

Suppose you have a DataFrame of one million retail transactions with columns for customer_age, purchase_amount, and transaction_timestamp, and roughly five percent of customer_age values are missing. If age is missing completely at random, dropping those rows leaves nine hundred fifty thousand records and is safe for a quick aggregate report. If you are training a churn model where age is predictive, dropping rows loses labeled outcomes and reduces model performance; instead you might fill with the median age of that customer segment, or add an is_age_missing indicator column so the model learns whether missingness itself carries signal. For the timestamp column, a missing value would be NaT, and you could either drop the row if time is essential or infer it from the order of a sorted index.

Interview question

A pandas DataFrame has 5% missing values in customer_age, completely at random. When is dropna the better choice over fillna?

  • a.When building a churn model where age is predictive and each row contains a labeled outcome
  • b.When the column uses nullable Int64 dtype to avoid coercion to float64
  • c.When generating a quick aggregate report and the remaining 95% of data is sufficientCorrect
  • d.When you want to add an is_age_missing indicator column to preserve missingness signal
Why?

The card states that dropna is safe for a quick aggregate report when data is missing completely at random and the remaining sample is still large enough. Option A describes the opposite scenario: when age is predictive, dropping rows discards valuable labeled outcomes and reduces model performance, so imputation is preferred instead.

Just read this? Test yourself on what you have been reading.

Read the original → pandas.pydata.org

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Every open role lists the topics its interview covers, so you can prepare for the real thing rather than guessing.

See open roles