"isna()" Function in Python

The isna() method in Python is a robust data manipulation and analysis toolbox that is widely utilized while working with pandas. The isna() function finds missing or null values in a pandas DataFrame or Series.

The usage of the isna() function in various scenarios is as follows:

  • In DataFrame(DataFrame.isna())

The isna() method returns a boolean-valued DataFrame of the same shape when applied to a Pandas DataFrame. The corresponding element in the original DataFrame is displayed for each element in the generated DataFrame as NaN (not present). The corresponding element in the resulting DataFrame is True if one of the elements is NaN; If not, it is false.

Example code snippet:

Output:

Column1  Column2
0    False    False
1    False     True
2     True    False
3    False    False
  • In Series(Serries.isna())

When applied to a pandas Series, the isna() method returns a Series of boolean values. Each resultant Series member represents whether or not the matching element in the original Series is NaN.

Example:

Output:

0    False
1    False
2     True
3    False
dtype: bool

Explanation:

When dealing with missing data, the isna() function comes in handy because it allows you to locate NaN values within your DataFrame or Series. After that, this information can be used to handle or manipulate the missing data as needed.

  • DataFrame Output: When isna() is applied to a DataFrame, the new DataFrame has the same shape (rows and columns) as the original DataFrame. The generated DataFrame's values will be boolean (True or False), indicating whether or not the corresponding element in the original DataFrame is missing.
  • Handling Non-Numeric Data: The isna() function accepts numeric and non-numeric data, such as texts or categorical variables. Missing non-numeric values in pandas are represented as NaN or None, and the isna() method correctly identifies them.
  • Handling Different Missing Value Representations: In addition to NaN and None, a panda recognizes alternative missing value representations, such as NaT (Not a Time) for missing timestamps or NaT (Not a Date) for missing dates. The isna() function is intended to handle these various forms and reliably indicate missing values.
  • Handling Missing Values in Time Series Data: When working with time series data, pandas has functions such as isnull() and isna(). Both functions are interchangeable and equivalent. They act similarly to the isna() method in that they assist you in identifying missing values in time series data.
  • After using isna() to identify missing values, you may use other pandas functions to handle or alter them. Dropna() (to drop rows or columns with missing values), fillna() (to fill missing values with specified values), and interpolate() (to interpolate missing values based on existing data) are examples of these functions.
  • Counting missing values: In addition to using isna() to check for missing values, you can use the sum() method to tally the number of missing values in each column or row.

Example:

Output:

Column1    1
Column2    1
dtype: int64