search
Search
Math ML
Map of Data Science
Guest 0reps
Thanks for the thanks!
close
account_circle
Profile
exit_to_app
Sign out
search
keyboard_voice
close
Searching Tips
Search for a recipe:
"Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
Doc Search
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Shrink
Navigate to
A
A
brightness_medium
share
arrow_backShare

# Counting number of rows with missing values in Pandas DataFrame

Pandas
chevron_right
Cookbooks
chevron_right
DataFrame Cookbooks
chevron_right
Handling Missing Values
schedule Jul 1, 2022
Last updated
local_offer PythonPandas
Tags
expand_more

# At least one missing value

Consider the following DataFrame:

``` df = pd.DataFrame({"A":[np.nan,3,np.nan],"B":[4,5,6],"C":[np.nan,7,8]}, index=["a","b","c"])df A B Ca NaN 4 NaNb 3.0 5 7.0c NaN 6 8.0 ```

## Solution

To count the number of rows that contain at least one missing value:

``` df.isna().any(axis=1).sum() 2 ```

## Explanation

We first use `isna()` method to get a DataFrame of booleans where `True` indicates the presence of `NaN`:

``` df.isna() A B Ca True False Trueb False False Falsec True False False ```

We then use `any(axis=1)`, which returns a Series of booleans where `True` indicates a row with at least one `True`:

``` df.isna().any(axis=1) a Trueb Falsec Truedtype: bool ```

In Pandas, `True` is internally represented as a `1`, while `False` as a `0`. Therefore, taking the sum of this Series will return the number of rows with at least one missing value:

``` df.isna().any(axis=1).sum() 2 ```

# With all missing values

Consider the following DataFrame:

``` df = pd.DataFrame({"A":[np.nan,np.nan],"B":[3,np.nan]}, index=["a","b"])df A Ba NaN 3.0b NaN NaN ```

## Solution

To get the number of rows with all missing values:

``` df.isna().all(axis=1).sum() 1 ```

## Explanation

Once again, we first use `isna()` to get a DataFrame of booleans where `True` indicates the presence of `NaN`:

``` df.isna() A Ba True Falseb True True ```

Next, we use `all(axis=1)` to get a Series of booleans where `True` indicates a row with all `True`s:

``` df.isna().all(axis=1) a Falseb Truedtype: bool ```

In Pandas, `True` is internally represented as a `1`, while `False` as a `0`, so taking the summation tells us the number of rows with all missing column values:

``` df.isna().all(axis=1).sum() 1 ```
mail
Edited by 0 others
thumb_up
thumb_down
thumb_up
2
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!