How pandas isna works in Python? Best example

How pandas isna works in Python? Best example
“`html

Understanding pandas.isna() in Python

When working with data in Python, handling missing values is a crucial part of the process. One of the most commonly used functions for detecting missing values in a dataset is pandas.isna(). If you’ve ever wondered, “How pandas isna works in Python? Best example”, you’re in the right place.

What is pandas.isna()?

The pandas.isna() function is a simple yet powerful tool that allows us to identify missing values (i.e., NaN, None, or null) in a DataFrame or Series. It returns a boolean mask where each element is True if it is missing and False otherwise.

Basic Syntax of pandas.isna()

The syntax is straightforward:

import pandas as pd

pd.isna(data)

Where data can be a Series, DataFrame, or even a scalar value.

How pandas isna Works in Python?

Let’s break it down with practical examples.

Example 1: Detecting Missing Values in a Series

import pandas as pd

data = pd.Series([1, 2, None, 4, float("nan")])

print(pd.isna(data))

This will output:

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

Here, None and NaN are correctly recognized as missing values.

Example 2: Finding Missing Values in a DataFrame

If we apply pandas.isna() to a DataFrame, it will check for missing values in all columns.

data = pd.DataFrame({
    "A": [1, 2, None, 4],
    "B": [None, 5, 6, 7],
    "C": [8, None, 10, None]
})

print(pd.isna(data))

Output:

       A      B      C
0  False   True  False
1  False  False   True
2   True  False  False
3  False  False   True

Now we can see exactly where missing values are present in the dataset.

Example 3: Using pandas.isna() with Conditional Statements

We can also use isna() in combination with conditional statements to filter out missing values.

missing_values = data[pd.isna(data["B"])]
print(missing_values)

This will return only the rows where column B has missing values.

What Data Types Does pandas.isna() Work With?

The function works with:

  • Series – It checks each element in a Series for missing values.
  • DataFrame – It creates a boolean mask with the same structure.
  • Scalar values – It returns True or False based on whether the single value is NaN.

Comparison: pandas.isna() vs. pandas.isnull()

Both pandas.isna() and pandas.isnull() perform the same function. They are interchangeable:

Function Purpose
pd.isna() Checks for NaN or None values
pd.isnull() Alias for isna(), works the same way

Handling Missing Data After Detection

Once we detect missing values using isna(), we can handle them using various techniques:

  1. Drop rows/columns – Use dropna().
  2. Fill missing values – Use fillna().
  3. Replace missing data – Use replace().

Conclusion

Now you know how pandas isna works in Python with practical examples. It’s an essential function when working with real-world datasets where missing values are inevitable. Mastering this function will help you clean and preprocess data effectively, making your analysis more reliable.

“` Other interesting article: How pandas to_numeric works in Python? Best example