liveBook · Manning

Dropna

As explained in Pandas Workout: 200 exercises to make you a stronger data analyst

dropna is a pandas method that removes missing values or rows and columns containing them according to specified criteria.

`dropna` filters missing data from a pandas Series or DataFrame. By default, calling `df.dropna()` returns a new DataFrame containing only rows with no `NaN` values. Treat that default as a strict completeness test: it is useful before an operation that cannot accept missing values, but it can discard more data than expected. section 2.20 section 3.10

all_good_df = df.dropna()

Use the default form to create a cleaned result while retaining the original DataFrame for comparison. section 5.2

The method returns a new object rather than changing the original one. That result can share underlying data, so make an explicit copy before assigning into a DataFrame produced by `dropna`; otherwise, later assignments can produce `SettingWithCopyWarning` or fail to stick. section 2.20 section 3.10

Prepare nullable fields for integer conversion

Taxi records with missing `passenger_count` or `payment_type` cannot be converted directly to integer dtypes. First remove rows with missing values, copy the result, and then convert the columns. This is reasonable only after checking how common the missing records are and deciding that exclusion fits the task. section 3.10

Default filtering is often too aggressive. Limit the decision to columns required for the analysis with `subset`, such as identifiers and location fields. Add `thresh` to keep a row when it has at least a chosen number of non-missing values within that subset. These options turn “complete row” into a deliberate data-quality rule. section 2.20 section 5.2

Check the consequence of dropping data rather than treating cleanup as automatic:

  • Compare original and filtered row counts to measure how many records were excluded. section 5.2
  • Consider the practical cost of excluded records; a small percentage can still represent a large amount of information or value. section 5.2
  • Count missing values before filtering when a conversion depends on a field being present. section 5.6

Keep rows needed for a focused question

To calculate an average age, remove missing values only from the `age` column with `df.dropna(subset=['age'])`. Missing values elsewhere do not prevent that calculation. Afterward, further validation may still be needed, because non-missing strings can be nonnumeric or implausible. section 5.6

`dropna` also cleans derived results. Dividing two indexed Series can create null values where indexes do not align; dropping those values yields a usable ranking. Similarly, after grouped descriptive statistics produce an all-null category, `dropna()` can remove that incomplete row before reshaping the result. section 8.2 section 13.3

A filtered result can be empty even though its columns remain intact: if every row has at least one missing value, default `dropna()` leaves zero rows. Inspect the result’s shape and choose a subset or threshold when total removal would make the analysis meaningless. section 5.2

How Pandas Workout: 200 exercises to make you a stronger data analyst covers it

How deep it goes
Explains default filtering, returned-object behavior, copy safety, subset selection, and threshold-based retention without pandas internals.
Examples
Uses taxi rides, parking violations, celebrity ages, developer-survey country rankings, and grouped university tuition statistics.
What it assumes
Assumes familiarity with pandas DataFrames, Series, columns, indexing, and basic dtype conversion.
What you can do after
Apply it via pandas to choose, measure, and safely implement missing-data exclusion rules for an analysis.
How it connects
Connects missing values with NaN, fillna, copy, SettingWithCopyWarning, astype, indexes, groupby, and transpose.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Pandas Workout: 200 exercises to make you a stronger data analyst. The section links point to the parts of the book it draws on.