Let’s say we have the following DataFrame with missing values:
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
Here, NaN indicates a missing value. Now, we want to find the total number of missing values in each column of the DataFrame. We can use the following Python code to find out the same:
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()) print("Total missing values: \n", df1.isnull().sum())
The output of the above program will be like the following:






0 Comments