Coding Ref

What is isna() in Pandas?

What is isna() in Pandas?

The isna() function in Pandas is used to check for null or missing values in a DataFrame or Series.

This function returns a boolean value for each element in the DataFrame or Series, indicating whether the element is null or missing.

Check for null values

Here's an example of using the isna() function in Pandas to check for null values:

main.py
import pandas as pd

# create a sample DataFrame
df = pd.DataFrame({"A": [1, 2, 3, 4, 5],
                   "B": [None, 7, 8, 9, 10]})

# check for null values in the DataFrame
df_isna = df.isna()

# display the result
print(df_isna)

This will check for null values in the DataFrame, and return a new DataFrame with a boolean value for each element indicating whether the element is null or not.

The output will be:

output
       A      B
0  False   True
1  False  False
2  False  False
3  False  False
4  False  False

Check for missing values in a series

You can also use the isna() function to check for missing values in a series.

For example:

main.py
# create a sample series
s = pd.Series([1, 2, None, 4, 5])

# check for missing values in the series
s_isna = s.isna()

# display the result
print(s_isna)

This will check for missing values in the series, and return a new series with a boolean value for each element indicating whether the element is missing or not.

The output will be:

output
0    False
1    False
2     True
3    False
4    False
dtype: bool

Conclusion

The isna() function is useful for checking for null or missing values in a dataframe or series, and it returns a boolean value for each element indicating whether the element is null or missing.

You'll also like

Related tutorials curated for you

    How to sort a series in Pandas

    How to create a bar chart in Pandas

    How to get the number of columns in a Pandas DataFrame

    How to find the mode in a Pandas DataFrame

    How to change the order of columns in Pandas

    What is nlargest() in Pandas?

    How to reshape a Pandas DataFrame

    How to drop an index column in Pandas

    How to use ffill() in Pandas

    How to write a Pandas DataFrame to SQL

    How to give multiple conditions in loc() in Pandas

    How to split a Pandas DataFrame by a column value