Pandas: Handling Missing value NaN

Write a Pandas program to select rows where score is missing i.e. NaN. with Practical Example

Program Logic:

  • Create Dictionary say ‘marks‘ which store marks of 5 subject and score of subjects
  • Create Data Frame say ‘result‘ which store the marks of 5 students using DataFrame method.
  • Display Data frame ‘result‘ using print function.
  • Select the score column of data frame ‘result’ and apply isnull function on it.
  • Display output using print function
  • Exit.

Below is implementation code/Source code

Here is program to select rows that have missing values.The output is also shown

import pandas as pd
import numpy as np
marks  = { "English" :[67,89,90,55],
           "Maths":[55,67,45,56],
            "IP":[66,78,89,90],
           "Chemistry" :[45,56,67,65],
           "Biology":[54,65,76,87],
           "score" :[12.6,np.nan,16.7,np.nan]}
result = pd.DataFrame(marks,index=["Athang","Sujata","Sushil","Sumedh"])
print("******************Marksheet****************")
print(result)
print("Rows where score is missing:")
print(result[result['score'].isnull()])

Output:

****************Marksheet********************
English Maths IP Chemistry Biology score
Athang 67 55 66 45 54 12.6
Sujata 89 67 78 56 65 NaN
Sushil 90 45 89 67 76 16.7
Sumedh 55 56 90 65 87 NaN
Rows where score is missing:
English Maths IP Chemistry Biology score
Sujata 89 67 78 56 65 NaN
Sumedh 55 56 90 65 87 NaN

Below is snapshot of executable code with output

Snapshot of source code
Snapshot of output

You can Also check other pandas program

Pandas program to find and replace the missing values in given Data Frame which do not have any valuable information

Pandas program to select rows where score is between 15 and 20

<