Let’s say we have the following DataFrame:
df1: Column 1 Column 2 Column 3 Column 4 Row 1 1.0 2.0 NaN 4.0 Row 2 5.0 6.0 7.0 8.0 Row 3 9.0 NaN 11.0 12.0 Row 4 NaN 14.0 15.0 16.0 Row 5 NaN 18.0 19.0 NaN
The DataFrame has some missing values that are indicated using NaN. We want to calculate the percentage of missing values in each column of the DataFrame. So, how should we do that? In this article, we will discuss that in detail.
Pandas DataFrame has a function DataFrame.isnull(). This function returns True if a value is null and False otherwise. So, we can use the following Python code to see the output of the DataFrame.isnull() function of the given DataFrame.
import pandas import numpy list1 = [[1, 2, numpy.nan, 4], [5, 6, 7, 8], [9, numpy.nan, 11, 12], [numpy.nan, 14, 15, 16], [numpy.nan, 18, 19, numpy.nan]] df1 = pandas.DataFrame(list1, index=["Row 1", "Row 2", "Row 3", "Row 4", "Row 5"], columns=["Column 1", "Column 2", "Column 3", "Column 4"]) print("df1: \n", df1) print("df1.isnull: \n", df1.isnull())
The output of the above program will be like the following:






0 Comments