search
Search
Unlock 100+ guides
search toc
close
Cancel
Post
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

# Counting number of rows with missing values in Pandas DataFrame

schedule Aug 12, 2023
Last updated
local_offer
PythonPandas
Tags
expand_more
mode_heat
Master the mathematics behind data science with 100+ top-tier guides
Start your free 7-days trial now!

# 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 ```
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...
thumb_up
5
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!