遍历pandas DataFrame中每一列的函数

时间:2018-09-27 21:03:13

标签: python python-3.x pandas function

您好,我在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")

****图片****

enter image description here

0 个答案:

没有答案