w3resource

Pandas Series: notna() function

Detect existing values in Pandas series

The notna() function is used to detect existing (non-missing) values.

Return a boolean same-sized object indicating if the values are not NA. Non-missing values get mapped to True. Characters such as empty strings '' or numpy.inf are not considered NA values (unless you set pandas.options.mode.use_inf_as_na = True). NA values, such as None or numpy.NaN, get mapped to False values.

Syntax:

Series.notna(self)
Pandas Series notna image

Returns: Series- Mask of bool values for each element in Series that indicates whether an element is not an NA value.

Example - Show which entries in a DataFrame are not NA:

Python-Pandas Code:

import numpy as np
import pandas as pd
df = pd.DataFrame({'age': [6, 7, np.NaN],
                   'born': [pd.NaT, pd.Timestamp('1998-04-25'),
                             pd.Timestamp('1940-05-27')],
                   'name': ['Alfred', 'Spiderman', ''],
                   'toy': [None, 'Spidertoy', 'Joker']})
df

Output:

     age	born	  name	    toy
0	 6.0	NaT	     Alfred	   None
1	 7.0 1998-04-25	Spiderman Spidertoy
2	NaN	 1940-05-27		       Joker

Python-Pandas Code:

import numpy as np
import pandas as pd
df = pd.DataFrame({'age': [6, 7, np.NaN],
                   'born': [pd.NaT, pd.Timestamp('1998-04-25'),
                             pd.Timestamp('1940-05-27')],
                   'name': ['Alfred', 'Spiderman', ''],
                   'toy': [None, 'Spidertoy', 'Joker']})
df.notna()

Output:

   age	born	name	toy
0	True	False	True	False
1	True	True	True	True
2	False	True	True	True

Example - Show which entries in a Series are not NA:

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series([6, 7, np.NaN])
s

Output:

0    6.0
1    7.0
2    NaN
dtype: float64
Pandas Series notna image

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series([6, 7, np.NaN])
s.notna()

Output:

0     True
1     True
2    False
dtype: bool

Previous: Detect missing values in the given Pandas series
Next: Analyze and drop Rows/Columns with Null values in a Pandas series



Follow us on Facebook and Twitter for latest update.