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

Fine tuning a Hugging Face transformer model for multi-track music generation

To train a PyTorch Transformer for multi-track music generation, the standard paradigm is to convert…

3 weeks ago

what are Transformer, LLM , and GPT

In the field of AI and Natural Language Processing (NLP), The Transformer is the engine…

3 weeks ago

Main framework for PyTorch music generation model

If your goal is purely symbolic melody generation (e.g., generating MIDI files or notes rather…

4 weeks ago

affine_grid() in pytorch

According to PyTorch document, In PyTorch, torch.nn.functional.affine_grid generates a 2D or 3D flow field (sampling…

3 months ago

How to create an Android mobile app with a deep learning AI model ?

Creating an Android mobile app with a deep learning AI model involves several key steps:…

12 months ago

Download source files for R Machine learning

Click here to go to source files for R Machine Learning

1 year ago