search
Search
Login
Unlock 100+ guides
menu
menu
web
search toc
close
Comments
Log in or sign up
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
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
check_circle
Mark as learned
thumb_up
0
thumb_down
0
chat_bubble_outline
0
Comment
auto_stories Bi-column layout
settings

Getting integer indexes of rows with NaN in Pandas DataFrame

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

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])
robocat
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Comment
Citation
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!