Sunday 19 November 2023

Leveraging Pandas isnull() to Identify Missing Values

‘isnull’ method is used to check for missing values in a DataFrame. It return a Boolean mask, where the value is True for the column with missing value and False for the column with some value.

Let’s experiment with below data set.

 

Example

       Name  Age       City  Gender  Percentage
0   Krishna   34  Bangalore    Male         NaN
1     Sailu   35       None  Female        76.0
2      Joel   29  Hyderabad    Male        67.0
3     Chamu   35    Chennai  Female       100.0
4  Jitendra   52  Bangalore    Male         NaN
5       Raj   34       None    Male        89.0

 

Example 1: Get all the persons whose City do not have any information.

city_info_missed = df['City'].isnull()
city_info_missed_rows = df[city_info_missed]

Example 2: Get all the persons whose Percentage is missing.

percentage_info_missed = df['Percentage'].isnull()
percentage_info_missed_rows = df[percentage_info_missed]

Find the below working application.

 

is_null.py

import pandas as pd
import numpy as np

# Create a sample DataFrame
data = {'Name': ['Krishna', 'Sailu', 'Joel', 'Chamu', 'Jitendra', "Raj"],
        'Age': [34, 35, 29, 35, 52, 34],
        'City': ['Bangalore', None, 'Hyderabad', 'Chennai', 'Bangalore', None],
        'Gender': ['Male', 'Female', 'Male', 'Female', 'Male', 'Male'],
        'Percentage': [np.nan, 76, 67, 100, np.nan, 89]}

df = pd.DataFrame(data)
print('Original DataFrame')
print(df)

print('\nGet all the persons whose City do not have any information')
city_info_missed = df['City'].isnull()
city_info_missed_rows = df[city_info_missed]
print(city_info_missed_rows)

print('\nGet all the persons whose Percentage do not have any information')
percentage_info_missed = df['Percentage'].isnull()
percentage_info_missed_rows = df[percentage_info_missed]
print(percentage_info_missed_rows)

Output

Original DataFrame
       Name  Age       City  Gender  Percentage
0   Krishna   34  Bangalore    Male         NaN
1     Sailu   35       None  Female        76.0
2      Joel   29  Hyderabad    Male        67.0
3     Chamu   35    Chennai  Female       100.0
4  Jitendra   52  Bangalore    Male         NaN
5       Raj   34       None    Male        89.0

Get all the persons whose City do not have any information
    Name  Age  City  Gender  Percentage
1  Sailu   35  None  Female        76.0
5    Raj   34  None    Male        89.0

Get all the persons whose Percentage do not have any information
       Name  Age       City Gender  Percentage
0   Krishna   34  Bangalore   Male         NaN
4  Jitendra   52  Bangalore   Male         NaN

 

Previous                                                 Next                                                 Home

No comments:

Post a Comment