search
Search
Unlock 100+ guides
search toc
close
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
What does this mean?
Why is this true?
Give me some examples!
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

# Getting index of rows with missing values (NaNs) 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!

# Getting index (row label)

Consider the following DataFrame with some missing values:

``` df = pd.DataFrame({"A":[3,pd.np.NaN,5],"B":[6,7,pd.np.NaN]}, index=["a","b","c"])df A Ba 3.0 6.0b NaN 7.0c 5.0 NaN ```

## Solution

To get the index of rows with missing values in Pandas optimally:

``` temp = df.isna().any(axis=1)temp[temp].index Index(['b', 'c'], dtype='object') ```

## Explanation

We first check for the presence of `NaN`s using `isna()`, which returns a DataFrame of booleans where `True` indicates the presence of a `NaN`:

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

Next, we use `any(axis=1)`, which scans each row and returns a Series of booleans where `True` indicates a row with at least one `True`:

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

We then temporarily store this intermediate result in a variable called `temp`. Our goal now is to extract the Index where the corresponding value is `True` (`b` and `c` in this case).

We first exclude indexes where the corresponding values is `False` by treating `temp` as a boolean mask:

``` temp[temp] b Truec Truedtype: bool ```

Finally, all we need to do is to access the index property of this Series:

``` temp[temp].index Index(['b', 'c'], dtype='object') ```

# Getting integer index

Again, consider the same `df` as above:

``` df = pd.DataFrame({"A":[3,pd.np.NaN,5],"B":[6,7,pd.np.NaN]}, index=["a","b","c"])df A Ba 3.0 6.0b NaN 7.0c 5.0 NaN ```

## Solution

To get the integer indexes of rows with missing values:

``` np.where(df.isna().any(axis=1))[0] # returns a NumPy array array([1, 2]) ```

## Explanation

Similar to the case above, we start by checking for the presence of `NaN` values using `isna()`:

``` df.isna() # returns a DataFrame A Ba False Falseb True Falsec False True ```

We then check for rows where there is at least one `True`:

``` df.isna().any(axis=1) # returns a Series a Falseb Truec Truedtype: bool ```

To get the integer index of the boolean `True`, use `np.where(~)`:

``` np.where(df.isna().any(axis=1)) # returns a tuple of size one (array([1, 2]),) ```

Here, `np.where(~)` returns a tuple of size one, and so we use `[0]` to extract the NumPy array of indexes:

``` np.where(df.isna().any(axis=1))[0] # returns a NumPy array array([1, 2]) ```
Edited by 0 others
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...
thumb_up
1
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!