In Python programming, NaN (not a number) values denotes those missing values, and values that not available among various calculations, such as divided by zero or logarithm of a negative number. Pandas allows to assign NaN values to Series and Data Frames. Two useful functions isnull() and notnull() will return boolean object such that we can filter original Series based on TRUE or FALSE values. Next we show some examples of how to deal with NaN values in Pandas Series.
#Import Pandas and Numpy modules
import pandas as pd
import numpy as np
#Create a Series, with one NaN value
T = pd.Series([19, 32, 7, np.NaN, 301])
T
#output
0 19.0
1 32.0
2 7.0
3 NaN
4 301.0
dtype: float64
#isnull() to return a boolean Series of same dimension
T.isnull()
#result
0 False
1 False
2 False
3 True
4 False
dtype: bool
#We can use isnull() to filter out NaN values only
T[T.isnull()]
#result is a new Series
3 NaN
dtype: float64
#notnull() returns boolean Series of same dimension
#if the value is not a NaN, then is TRUE, otherwise FALSE
T.notnull()
#result is a boolean Series of same dimension
0 True
1 True
2 True
3 False
4 True
dtype: bool
#notnull() can also be used to filter out values that
#are not NaN values, result is a Series.
T[T.notnull()]
#Output
0 19.0
1 32.0
2 7.0
4 301.0
dtype: float64
Click here to download Python Course Source Files !
For online Python training registration, click here ! Pandas provides flexible ways of generating data…
For online Python training registration, click here ! Data frame is the tabular data object…
Click her for course registration ! When a data frame in Python is created via…
We provide affordable online training course(via ZOOM meeting) for Python and R programming at fundamental…