How to Use np.nan in Python

The np.nan is a constant representing a missing or undefined numerical value in a NumPy array. It stands for not a number and has a float type.

Syntax

numpy.nan

Example 1: Usage of np.nan() Method

import numpy as np

print(np.nan)

Output

nan

Example 2: Numpy array with nan value

Numpy array with nan value
Figure 1: Numpy array with nan value
import numpy as np

myarr = np.array([1, 0, np.nan, 3])

print(myarr)

Output

[ 1. 0. nan 3.]

The important thing I would like you to take away from this is that all of our integers have been converted to floats, and that’s because NumPy has defined the NaN data type as a float.

Example 3: Comparing np.nan values

You can use the “double equal(==)” operator to compare two nan values in Python.

Comparing np.nan values
Figure 2: Comparing np.nan values
import numpy as np

print(np.nan == np.nan)

Output

False

You can see that two np.nan values are not same.

Example 4: Checking for NaN values

To check for NaN values in an array, use the np.isnan() method.

Syntax

np.isnan(arr)

Visual representation

Checking for NaN values
Figure 3: Checking for NaN values

Here is an example.

import numpy as np

arr = np.nan
arr2 = 1

print(np.isnan(np.nan))
print(np.isnan(arr2))

Output

True
False

Example 5: NaN in Pandas Dataframe

In pandas, “NaN” stands for “Not a Number” and is the standard missing data marker. It’s essentially Python marking data as “not present” or “unknown”.

import numpy as np
import pandas as pd

df = pd.DataFrame([(1.0, np.nan, -1.0, 21.0),
                   (np.nan, 12.0, np.nan, 11),
                   (21.0, 15.0, np.nan, 91.0),
                   (np.nan, 14.0, -31.0, 19.0)],
              columns=list('abcd'))

print(df)

Output

NaN in Pandas Dataframe

Example 6: Checking for NaN values in Pandas DataFrame

You can check for NaN values by using the isnull() method. The output will be a boolean mask with dimensions of the original dataframe.

import numpy as np
import pandas as pd

df = pd.DataFrame([(1.0, np.nan, -1.0, 21.0),
     (np.nan, 12.0, np.nan, 11),
     (21.0, 15.0, np.nan, 91.0),
     (np.nan, 14.0, -31.0, 19.0)],
     columns=list('abcd'))

print(pd.isnull(df))

Output

Checking for NaN values in Pandas DataFrame

Example 7: Replacing NaN values in DataFrame

To replace NaN values in a Pandas Dataframe, you can “use the df.fillna() method.”

import numpy as np
import pandas as pd

df = pd.DataFrame([(1.0, np.nan, -1.0, 21.0),
                   (np.nan, 12.0, np.nan, 11),
                   (21.0, 15.0, np.nan, 91.0),
                   (np.nan, 14.0, -31.0, 19.0)],
          columns=list('abcd'))

print(df.fillna(0))

Output

Replacing NaN values in DataFrame

Example 8: Dropping rows containing NaN values

To drop the rows or columns with NaNs, you can use the .dropna() method.

import numpy as np
import pandas as pd

df = pd.DataFrame([(1.0, np.nan, -1.0, 21.0),
  (np.nan, 12.0, np.nan, 11),
  (21.0, 15.0, np.nan, 91.0),
  (np.nan, 14.0, -31.0, 19.0)],
  columns=list('abcd'))

print(df.dropna(axis="columns"))

Output

    d
0  21.0
1  11.0
2  91.0
3  19.0

That’s it.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.