Pandas How To Uncategorized How to test for nan in Pandas

How to test for nan in Pandas

In Pandas, you can use the isna() method to check for NaN (Not a Number) values in a DataFrame or Series. Here’s an example:

import pandas as pd
import numpy as np

# create a sample DataFrame with NaN values
df = pd.DataFrame({'A': [1, 2, np.nan], 'B': [4, np.nan, 6], 'C': [7, 8, 9]})

# check for NaN values in the DataFrame
print(df.isna())

# check for NaN values in a specific column
print(df['A'].isna())

The isna() method returns a boolean DataFrame or Series indicating which values are NaN. If a value is NaN, the corresponding element in the output will be True, otherwise it will be False.

Alternatively, you can use the isnull() method, which is equivalent to isna().

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post