您好,我在Python中创建了一个函数,该函数将数据框作为第一个参数,将列作为第二个参数。目标是在该字段中查找空值的总数。我想遍历数据框中的每一列并应用相同的功能。谢谢!
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")
****图片****