Selecting Rows with NaN Values

Write a Pandas program to select the rows where the score is missing, i.e. is NaN.

Example 1:

Input: DataFrame with some 'score' values as NaN 
Output: Rows with 'score' as NaN

Example 2:

Input: DataFrame with some 'score' values as NaN 
Output: Rows with 'score' as NaN

Use the pandas isnull() function to identify rows with NaN values.

import pandas as pd
import numpy as np

def select_nan_rows(df):
    return df[df['score'].isnull()]

df1 = pd.DataFrame({'name': ['Tom', 'Jack', 'Steve', 'Ricky'], 'score': [85, np.nan, 87, 91]})
df2 = pd.DataFrame({'name': ['Adam', 'Eve', 'Stark', 'Rogers'], 'score': [88, 92, np.nan, 97]})

print(select_nan_rows(df1))
print(select_nan_rows(df2))

 

© Let’s Data Science

LOGIN

Unlock AI & Data Science treasures. Log in!