0

Hi, I created a function in Python that takes a dataframe as the first argument, and a column as the second argument.The goal is to find the total number of null values in that field. I'd like to loop over every column in the dataframe and apply the same function. Thanks!

def count_nulls(df, col_name):
col = df[col_name]
bool_col = pd.isna(col)
total_nulls = pd.Series.sum(bool_col)
total_rows = len(col)
pct_null = round((total_nulls / total_rows) * 100,2)
print("The","\'"+ str(col_name) + "\'", "field has", str(total_nulls), "nulls which account for",
      str(pct_null) + "% of all values")

****Image****

enter image description here

Mendelsohn
  • 13
  • 3
  • 4
    `df.isnull().sum()`? – ALollz Sep 27 '18 at 21:05
  • 1
    Possible duplicate of [How to count the NaN values in a column in Pandas DataFrame](https://stackoverflow.com/questions/26266362/how-to-count-the-nan-values-in-a-column-in-pandas-dataframe) – ALollz Sep 27 '18 at 21:07

0 Answers0