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 df1.isnull: Column 1 Column 2 Column 3 Column 4 Row 1 False False True False Row 2 False False False False Row 3 False True False False Row 4 True False False False Row 5 True False False True Total missing values: Column 1 2 Column 2 1 Column 3 1 Column 4 1 dtype: int64
Here, DataFrame.isnull() function returns True if a value is NaN and False otherwise. So, df1.isnull() returns the following:
df1.isnull: Column 1 Column 2 Column 3 Column 4 Row 1 False False True False Row 2 False False False False Row 3 False True False False Row 4 True False False False Row 5 True False False True
So, finding out the total number of missing values in each column of the DataFrame is now easier. We can count the number of True in each column and the sum will indicate the total number of missing values in each column.
print("Total missing values: \n", df1.isnull().sum())
Now, the DataFrame.sum() function returns the sum of the values of each column of the DataFrame. So, if we call the function df1.isnull().sum(), it will return the number of True values in each column. This, in turn, will show the number of missing values in each column of the DataFrame.






0 Comments