search
Search
Login
Math ML Join our weekly DS/ML newsletter
menu
menu search toc more_vert
Robocat
Guest 0reps
Thanks for the thanks!
close
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
help Ask a question
Share on Twitter
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
icon_star
Doc Search
icon_star
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Navigate to
A
A
brightness_medium
share
arrow_backShare
Twitter
Facebook

Getting integer indexes of rows with NaN 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

Rows with missing value for a specific column

Consider the following DataFrame with some missing values:

import numpy as np
df = pd.DataFrame({"A":[3,np.nan,np.nan],"B":[5,6,np.nan]}, index= ["a","b","c"])
df
A B
a 3.0 5.0
b NaN 6.0
c NaN NaN

Solution

To get the integer indexes of rows where the value for column A is missing:

np.where(df["A"].isna())[0] # returns a NumPy array
array([1, 2])

Explanation

We first call isna() to extract a Series of booleans where True indicates rows with missing value(s) for column A:

df["A"].isna()
a False
b True
c True
Name: A, dtype: bool

We then call NumPy's where(~), which returns a tuple containing the integer indexes of entries that are True:

np.where(df["A"].isna())
(array([1, 2]),)

Finally, we use [0] to access the NumPy array of integer indexes within the tuple.

Rows with all missing values

Consider the following DataFrame:

import numpy as np
df = pd.DataFrame({"A":[3,np.nan,np.nan],"B":[5,6,np.nan]}, index= ["a","b","c"])
df
A B
a 3.0 5.0
b NaN 6.0
c NaN NaN

Solution

To get the integer indexes of rows with all missing values:

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

Explanation

We first obtain a DataFrame of booleans where True represents entries with missing values using isna():

df.isna()
A B
a False False
b True False
c True True

We then call all(axis=1), which returns a Series of booleans where True indicates a row with all True:

df.isna().all(axis=1)
a False
b False
c True
dtype: bool

We pass this into NumPy's where(~) method, which returns a tuple containing the integer indexes of entries that are True:

np.where(df.isna().all(axis=1))
(array([2]),)

We then access the integer indexes, which is a NumPy array, using [] notation:

np.where(df.isna().all(axis=1))[0]
array([2])
mail
Join our newsletter for updates on new DS/ML comprehensive guides (spam-free)
robocat
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Ask a question or leave a feedback...
thumb_up
0
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!