🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpandas

pandas Documentation

LOADING ENGINE...

df.dropna()

AI & DATA SCIENCE // df-dropna

df.dropna() removes rows (or columns) that contain any missing (NaN) values, returning a new DataFrame by default.

Syntax

df.dropna(axis=0, how='any', subset=None)

Deep Dive Course

By default, dropna() drops any row containing at least one NaN value anywhere in it, how='any'; passing how='all' instead only drops rows where every single value is NaN. The subset parameter restricts the missing-value check to specific columns, so a NaN in an unrelated column doesn't cause a row to be dropped. Passing axis=1 drops columns with missing values instead of rows, though dropping rows is by far the more common use.

1Understanding df.dropna()

By default, dropna() drops any row containing at least one NaN value anywhere in it, how='any'; passing how='all' instead only drops rows where every single value is NaN. The subset parameter restricts the missing-value check to specific columns, so a NaN in an unrelated column doesn't cause a row to be dropped. Passing axis=1 drops columns with missing values instead of rows, though dropping rows is by far the more common use.

💡

Use subset=['col1', 'col2'] with dropna() to only drop rows missing values in specific important columns, instead of dropping a row just because some unrelated, less critical column happens to have a NaN.

editor.html
import pandas as pd
import numpy as np

df = pd.DataFrame({"a": [1, np.nan, 3], "b": [4, 5, np.nan]})
print(df.dropna())
localhost:3000

2Practical Example

Here is a real-world application of df.dropna() showing how it is used in production Pandas code.

editor.html
import pandas as pd
import numpy as np

df = pd.DataFrame({"name": ["Alice", "Bob"], "email": ["a@x.com", np.nan]})
print(df.dropna(subset=["email"]))
localhost:3000

3Best Practices

Follow these guidelines when working with df.dropna():

1. Use subset to limit dropna()'s missing-value check to the columns that actually matter for your analysis, rather than dropping rows for missing values anywhere

2. Check how many rows dropna() would actually remove before applying it, to understand its real impact on the dataset

3. Prefer fillna() over dropna() when losing rows entirely isn't acceptable and a reasonable default/imputed value exists instead

⚠️

Tip: Use subset=['col1', 'col2'] with dropna() to only drop rows missing values in specific important columns, instead of dropping a row just because some unrelated, less critical column happens to have a NaN.

editor.html
import pandas as pd
import numpy as np

df = pd.DataFrame({"a": [1, np.nan, 3], "b": [4, 5, np.nan]})
print(df.dropna())
localhost:3000

Examples

Example 01Basic Usage
import pandas as pd
import numpy as np

df = pd.DataFrame({"a": [1, np.nan, 3], "b": [4, 5, np.nan]})
print(df.dropna())
Example 02Advanced Example
import pandas as pd
import numpy as np

df = pd.DataFrame({"name": ["Alice", "Bob"], "email": ["a@x.com", np.nan]})
print(df.dropna(subset=["email"]))

Best Practices

  • Use subset to limit dropna()'s missing-value check to the columns that actually matter for your analysis, rather than dropping rows for missing values anywhere
  • Check how many rows dropna() would actually remove before applying it, to understand its real impact on the dataset
  • Prefer fillna() over dropna() when losing rows entirely isn't acceptable and a reasonable default/imputed value exists instead

Interview Question

Why might using subset with dropna() be preferable to the default behavior of checking every column for missing values?

Hint: Think about which missing values actually matter for a given use case.

The default behavior drops a row if any column has a missing value, even one that's completely irrelevant to what you're actually doing with the data — a missing notes field, for example, might not matter at all if you only care about name and email. Passing subset=['email'] restricts the check to only the columns that genuinely matter for the task at hand, so rows are only dropped for missing values in those specific fields, preserving rows that have irrelevant gaps elsewhere that would otherwise be discarded unnecessarily.

Exercises

MediumPractice using df.dropna() in a real scenario.
View Solution
import pandas as pd
import numpy as np

df = pd.DataFrame({"a": [1, np.nan, 3], "b": [4, 5, np.nan]})
print(df.dropna())

Frequently Asked Questions

Why might using subset with dropna() be preferable to the default behavior of checking every column for missing values?

The default behavior drops a row if any column has a missing value, even one that's completely irrelevant to what you're actually doing with the data — a missing notes field, for example, might not matter at all if you only care about name and email. Passing subset=['email'] restricts the check to only the columns that genuinely matter for the task at hand, so rows are only dropped for missing values in those specific fields, preserving rows that have irrelevant gaps elsewhere that would otherwise be discarded unnecessarily.

Related Functions

df-fillnadf-isnadf-notna