YouTip LogoYouTip

Pandas Pd Isna

[![Image 1: Python math module](#) Pandas Common Functions](#) * * * `pd.isna()` is one of the most commonly used functions for detecting missing values in Pandas. It checks whether each element is a missing value and returns a boolean structure (`True` or `False`). In data analysis, handling missing values is a crucial first step. `pd.isna()` helps you quickly identify where missing values are located in your data, providing a basis for subsequent operations such as filling, deleting, or other processing. * * * ## Basic Syntax and Parameters ### Syntax Format pandas.isna(obj) ### Parameter Description | Parameter | Type | Description | | --- | --- | --- | | obj | DataFrame, Series, scalar, list, tuple | The object to check for missing values, which can be any supported data type. | ### Return Value Description * Returns a boolean object with the same structure as the input: * If input is DataFrame, returns a boolean DataFrame * If input is Series, returns a boolean Series * If input is a single value, returns a boolean scalar * * * ## Examples Let's go through a series of examples from simple to complex to fully master the usage of `pd.isna()`. ### Example 1: Detecting Missing Values in a Series ## Example import pandas as pd import numpy as np # Create a Series containing missing values s = pd.Series([1,2, np.nan,4,None,6]) # Detect missing values result = pd.isna(s) print("Original Series:") print(s) print("n Detection Result:") print(result) **Output:** Original Series:0 1.01 2.02 NaN3 4.04 NaN5 6.0 dtype: float64 Detection Result:0 False1 False2 True3 False4 True5 False dtype: bool **Code Explanation:** 1. Both `np.nan` and `None` are recognized as missing values 2. In the returned boolean Series, positions with missing values are `True`, and non-missing values are `False` ### Example 2: Detecting Missing Values in a DataFrame ## Example import pandas as pd import numpy as np # Create a DataFrame containing missing values df = pd.DataFrame({ 'A': [1,2, np.nan,4], 'B': ['a',None,'c','d'], 'C': [pd.NaT,6,7,8]# NaT is the missing value for datetime type }) # Detect missing values result = pd.isna(df) print("Original DataFrame:") print(df) print("n Detection Result:") print(result) **Output:** Original DataFrame: A B C 0 1.0 a None1 2.0 None 62 NaN c 73 4.0 d 8 dtype: object Detection Result: A B C 0 False False True1 False True False2 True False False3 False False False **Code Explanation:** * `pd.NaT` (Not a Time) is a missing value for datetime types and is correctly identified * Each position in the DataFrame returns a corresponding boolean value ### Example 3: Counting Missing Values In practical data analysis, we often need to know how many missing values there are. ## Example import pandas as pd import numpy as np # Create data df = pd.DataFrame({ 'A': [1, np.nan,3, np.nan,5], 'B': [np.nan,2, np.nan,4, np.nan], 'C': [1,2,3,4,5] }) # Count missing values per column print("Missing values per column:") print(pd.isna(df).sum()) print("n Total missing values:", pd.isna(df).sum().sum()) # Calculate proportion of missing values print("n Proportion of missing values:") print((pd.isna(df).sum() / len(df) * 100).round(2),"%") **Output:** Missing values per column: A 2 B 3 C 0 dtype: int64 Total missing values: 5 Proportion of missing values: A 40.0 B 60.0 C 0.0% ### Example 4: Using with Other Functions ## Example import pandas as pd import numpy as np # Create data df = pd.DataFrame({ 'name': ['Alice','Bob',None,'Diana','Eve'], 'age': [25,None,35,28,None], 'score': [85,90,78,92,88] }) # Use isna to filter rows with missing values rows_with_na = df[pd.isna(df).any(axis=1)] print("Rows with missing values:") print(rows_with_na) # Use isna to fill missing values df_filled = df.fillna({ 'name': 'Unknown', 'age': df['age'].mean(),# Fill age with mean value 'score': 0 }) print("n DataFrame after filling:") print(df_filled) **Output:** Rows with missing values: name age score 0 Alice 25.0 852 None 35.0 783 Diana 28.0 92 DataFrame after filling: name age score 0 Alice 25.0 851 Bob 29.0 902 Unknown 35.0 783 Diana 28.0 924 Eve 0.0 88 * * * ## Notes > **Important Note:** `pd.isna()` identifies the following values as missing: > > > * `np.nan` - Missing value for numeric types > * `None` - Native Python None object > * `pd.NaT` - Missing value for datetime types Empty strings `""` or `0` will return `False` when passed to `pd.isna()` because they are not considered missing values. * * Pandas Common Functions](#)
← Pandas Pd NotnaPandas Ecommerce β†’