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

Pandas pd.notna() function

Pandas pd.notna() function

Pandas Universal Functions Common Pandas functions


pd.notna() is a function in the Pandas library used toDetect missing valuesfunction. It checks each element in the input object to determine whether it is a non-missing value, and returns a result consisting of Boolean values (True/False).

This is one of the most commonly used functions in data cleaning; it helps us quickly identify valid values in the data and prepare for subsequent processing and analysis.

Word meaning: not is a negation,na is short for "not available"; together they mean "not a missing value".


Basic syntax and parameters

pd.notna() is a top-level function of the Pandas library; it can be called directly via pd.notna() , or via the .notna() method of a Series or DataFrame object.

Syntax format



pd.notna(obj)

Parameter Description

- Type: any Python object, such as a Series, DataFrame, list, array, or scalar value. - Description: the object to check for non-missing values. Missing values in Pandas include `None`, `NaN`, `NaT` (missing time type), and `pandas.NA`.

Function description


Example

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

Example 1: basic usage - detecting missing values in scalars and lists

Example

import

pandas

as

pd

import

numpy

as

np

1. Check whether a scalar value is non-missing

print

(

"=== scalar check ==="

)

print

(

f

"pd.notna(10): {pd.notna(10)}"

)

             

normal number -> True

print

(

f

"pd.notna('runoob'): {pd.notna('runoob')}"

)

 

normal string -> True

print

(

f

"pd.notna(None): {pd.notna(None)}"

)

         

None -> False

print

(

f

"pd.notna(np.nan): {pd.notna(np.nan)}"

)

     

np.nan -> False

2. Detect missing values in a list

print

(

"\n=== list check ==="

)

data_list

=

[

1

,

2

,

np.

nan

,

'runoob'

,

None

,

5

]

result

=

pd.

notna

(

data_list

)

print

(

f

"original list: {data_list}"

)

print

(

f

"detection result: {result.tolist()}"

)

 

Convert to a list for easier viewing

Expected output:



=== 标量检测 ===

pd.notna(10): True

pd.notna('runoob'): True

pd.notna(None): False

pd.notna(np.nan): False



=== 列表检测 ===

原始列表: [1, 2, nan, 'runoob', None, 5]

检测结果: [True, False, True, False, True]

Code analysis:

  1. pd.notna(10) and pd.notna('runoob') Detecting normal numbers and strings returns True。
  2. pd.notna(None) and pd.notna(np.nan) detecting Python and NumPy missing-value markers returns False。
  3. Using pd.notna() on a list returns a list of Booleans, with missing-value positions marked as False。

Example 2: detecting missing values in a Series

When working with Series data,pd.notna() can quickly locate valid data.

Example

import

pandas

as

pd

import

numpy

as

np

Create a Series containing missing values

s

=

pd.

Series

(

[

1

,

2

,

np.

nan

,

4

,

None

,

'runoob'

,

np.

nan

]

)

print

(

"=== Original Series ==="

)

print

(

s

)

print

(

f

"\nType: {type(s)}"

)

Detect using pd.notna()

print

(

"\n=== pd.notna() detection result ==="

)

result

=

pd.

notna

(

s

)

print

(

result

)

Using the Series notna() method (equivalent effect)

print

(

"\n=== s.notna() method detection ==="

)

print

(

s.

notna

(

)

)

Filter out non-missing values

print

(

"\n=== filter non-missing values ==="

)

print

(

s

[

s.

notna

(

)

]

)

Expected output:



=== 原始 Series ===

0       1

1       2

2     NaN

3       4

4    None

5    runoob

6     NaN

dtype: object



=== pd.notna() 检测结果 ===

0     True

1     True

2    False

3     True

4    False

5     True

6    False

dtype: bool



=== s.notna() 方法检测 ===

0     True

1     True

2    False

3     True

4    False

5     True

6    False

dtype: bool



=== 筛选非缺失值 ===

0       1

1       2

3       4

5    runoob

dtype: object

Code analysis:

Example 3: detecting missing values in a DataFrame

When working with tabular data,pd.notna() can quickly show how complete the data is.

Example

import

pandas

as

pd

import

numpy

as

np

Create a DataFrame with missing values

df

=

pd.

DataFrame

(

{

   

'name'

:

[

'Alice'

,

'Bob'

,

None

,

'Diana'

]

,

   

'age'

:

[

25

,

np.

nan

,

30

,

28

]

,

   

'score'

:

[

85

,

90

,

np.

nan

,

95

]

}

)

print

(

"=== Original DataFrame ==="

)

print

(

df

)

Detect missing values in the entire DataFrame

print

(

"\n=== pd.notna() detection result ==="

)

print

(

pd.

notna

(

df

)

)

Count missing values by column

print

(

"\n=== non-missing value count per column ==="

)

print

(

df.

notna

(

)

.

sum

(

)

)

Count missing values by row

print

(

"\n=== non-missing value count per row ==="

)

print

(

df.

notna

(

)

.

sum

(

axis

=

1

)

)

Compute the proportion of missing values

print

(

"\n=== missing value ratio ==="

)

missing_ratio

=

df.

isna

(

)

.

mean

(

)

print

(

missing_ratio

)

Expected output:



=== 原始 DataFrame ===

    name  age  score

0  Alice   25   85.0

1    Bob  NaN   90.0

2   None   30    NaN

3  Diana   28   95.0



=== pd.notna() 检测结果 ===

   name    age  score

0  True   True   True

1  True  False   True

2 False   True  False

3  True   True   True



=== 每列非缺失值数量 ===

name     3

age      3

score    3



=== 每行非缺失值数量 ===

0    3

1    2

2    1

3    3



=== 缺失值比例 ===

name     0.25

age      0.25

score    0.25

Code analysis:


Tip: pd.notna() The difference between pd.isna() are the exact opposite of each other.pd.notna() returns True wherepd.isna() returns False, and vice versa.

Pandas Common Functions Common Pandas functions