Categories: PandasPython

Handling NaN (not a number) values in Pandas Series using Python

We provide affordable online training course(via ZOOM meeting) for Python and R programming at fundamental level, click here for more details.

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

You can also watch videos on our YouTube channel for more understanding of Python programming skills.

wilsonzhang746

Recent Posts

Download R Course source files

Click here to download R Course source files !

2 months ago

Download Python Course source files

Click here to download Python Course Source Files !

2 months ago

How to create a data frame from nested dictionary with Pandas in Python

For online Python training registration, click here ! Pandas provides flexible ways of generating data…

5 months ago

How to delete columns of a data frame in Python

For online Python training registration, click here ! Data frame is the tabular data object…

5 months ago

Using isin() to check membership of a data frame in Python

Click her for course registration ! When a data frame in Python is created via…

5 months ago

How to assign values to Pandas data frame in Python

We provide affordable online training course(via ZOOM meeting) for Python and R programming at fundamental…

5 months ago