Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Pandas pd.isna() function

Pandas pd.isna() function

Python math Module Common Pandas functions


pd.isna() is the most commonly used missing-value detection function in Pandas. It checks whether each element is missing and returns a boolean (True or False) structure.

In data analysis, handling missing values is a very important first step.pd.isna() It helps you quickly identify where values are missing in the data, providing a basis for subsequent filling, deletion, or other processing operations.


Basic syntax and parameters

Syntax format



pandas.isna(obj)

Parameter Description

Parameters Type Description
obj DataFrame、Series、scalar、list、tuple The object to check for missing values, which can be any supported data type.

Return value description


Example

Let us thoroughly master pd.isna() through a series of examples from simple to complex.

Example 1: Detect missing values in a Series

Example

import

pandas

as

pd

import

numpy

as

np

Create a Series containing missing values

s

=

pd.

Series

(

[

1

,

2

,

np.

nan

,

4

,

None

,

6

]

)

Detect missing values

result

=

pd.

isna

(

s

)

print

(

"Original Series:"

)

print

(

s

)

print

(

"\nDetection result:"

)

print

(

result

)

Result:



原始 Series:

0    1.0

1    2.0

2    NaN

3    4.0

4    NaN

5    6.0

dtype: float64



Detection results:

0    False

1    False

2     True

3    False

4     True

5    False

dtype: bool

Code analysis:

  1. np.nan and None are all identified as missing values
  2. In the returned boolean Series, missing-value positions are True, and non-missing positions are False

Example 2: Detect missing values in a DataFrame

Example

import

pandas

as

pd

import

numpy

as

np

Create a DataFrame with missing values

df

=

pd.

DataFrame

(

{

   

'A'

:

[

1

,

2

,

np.

nan

,

4

]

,

   

'B'

:

[

'a'

,

None

,

'c'

,

'd'

]

,

   

'C'

:

[

pd.

NaT

,

6

,

7

,

8

]

 

NaT is the missing value for time types

}

)

Detect missing values

result

=

pd.

isna

(

df

)

print

(

"Original DataFrame:"

)

print

(

df

)

print

(

"\nDetection result:"

)

print

(

result

)

Result:



原始 DataFrame:

     A    B     C

0  1.0    a  None

1  2.0  None     6

2  NaN   c     7

3  4.0    d     8

dtype: object



Detection results:

       A      B      C

0  False  False   True

1  False   True  False

2   True  False  False

3  False  False  False

Code analysis:

Example 3: Count missing values

In real data analysis, we often need to know how many missing values there are.

Example

import

pandas

as

pd

import

numpy

as

np

Create data

df

=

pd.

DataFrame

(

{

   

'A'

:

[

1

,

np.

nan

,

3

,

np.

nan

,

5

]

,

   

'B'

:

[

np.

nan

,

2

,

np.

nan

,

4

,

np.

nan

]

,

   

'C'

:

[

1

,

2

,

3

,

4

,

5

]

}

)

Count the number of missing values in each column

print

(

"Missing value count per column:"

)

print

(

pd.

isna

(

df

)

.

sum

(

)

)

print

(

"\nTotal missing value count:"

,

pd.

isna

(

df

)

.

sum

(

)

.

sum

(

)

)

Compute the proportion of missing values

print

(

"\nMissing value proportion:"

)

print

(

(

pd.

isna

(

df

)

.

sum

(

)

/

len

(

df

)

100

)

.

round

(

2

)

,

"%"

)

Result:



Number of missing values per column:

A    2

B    3

C    0

dtype: int64



总缺失值数量: 5



Missing value ratio:

A    40.0

B    60.0

C     0.0

%

Example 4: Use in combination with other functions

Example

import

pandas

as

pd

import

numpy

as

np

Create data

df

=

pd.

DataFrame

(

{

   

'name'

:

[

'Alice'

,

'Bob'

,

None

,

'Diana'

,

'Eve'

]

,

   

'age'

:

[

25

,

None

,

35

,

28

,

None

]

,

   

'score'

:

[

85

,

90

,

78

,

92

,

88

]

}

)

Use isna to filter out rows containing missing values

rows_with_na

=

df

[

pd.

isna

(

df

)

.

any

(

axis

=

1

)

]

print

(

"Rows containing missing values:"

)

print

(

rows_with_na

)

Use isna to fill missing values

df_filled

=

df.

fillna

(

{

   

'name'

:

'Unknown'

,

   

'age'

: df

[

'age'

]

.

mean

(

)

,

 

Fill age with the mean value

   

'score'

:

0

}

)

print

(

"\nFilled DataFrame:"

)

print

(

df_filled

)

Result:



Rows containing missing values:

   name   age  score

0  Alice  25.0     85

2  None  35.0     78

3  Diana  28.0     92



填充后的 DataFrame:

     name   age  score

0    Alice  25.0     85

1      Bob  29.0     90

2  Unknown  35.0     78

3    Diana  28.0     92

4      Eve   0.0     88


Notes

Important note:pd.isna() recognizes the following values as missing:

  • np.nan - Numeric missing values
  • None - Python's native None object
  • pd.NaT - Missing values of time type (datetime)
  • For empty strings "" or 0,pd.isna() returns False, because they are not missing values.


    Python math Module Common Pandas functions