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.
import pandas as pd
import numpy as np
df = pd.DataFrame({"a": [1, np.nan, 3], "b": [4, 5, np.nan]})
print(df.dropna())2Practical Example
Here is a real-world application of df.dropna() showing how it is used in production Pandas code.
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"]))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.
import pandas as pd
import numpy as np
df = pd.DataFrame({"a": [1, np.nan, 3], "b": [4, 5, np.nan]})
print(df.dropna())