'isnotnan' functionality in numpy, can this be more pythonic?

Learn whether is there any functionality in numpy to check if a value is not a nan value? By Pranit Sharma Last updated : October 09, 2023

NumPy is an abbreviated form of Numerical Python. It is used for different types of scientific operations in python. Numpy is a vast library in python which is used for almost every kind of scientific or mathematical operation. It is itself an array which is a collection of various methods and functions for processing the arrays.

While creating a DataFrame or importing a CSV file, there could be some NaN values in the cells. NaN values mean "Not a Number" which generally means that there are some missing values in the cell.

'isnotnan' functionality in numpy

To check if a value is not a nan value, we will use isnan() method along with tilde(~) operator.

The tilde sign (~) in pandas is used when we work with Boolean values. In programming, we sometimes use some specific values that only have two values, either True or False. These values are known as Boolean values.

This tilde sign works as a negation operator that means is used to reverse the Boolean values.

Let us understand with the help of an example,

Python program to demonstrate the example of 'isnotnan' functionality in numpy

# Import numpy
import numpy as np

# Creating numpy array
arr = np.array([np.nan, 1, 2])

# Display original array
print("Orignal array:\n",arr,"\n")

# Check for each value
res = arr[~np.isnan(arr)]

# Display result


The output of the above program is:

Example: 'isnotnan' functionality in numpy, can this be more pythonic?

Python NumPy Programs »

Comments and Discussions!

Load comments ↻

Copyright © 2024 www.includehelp.com. All rights reserved.