理解python中的df.isnull.mean()

yas*_*hul 4 python python-3.x pandas

我有一个数据框 df。代码是这样写的

df.isnull().mean().sort_values(ascending = False)

这是输出的一部分-

inq_fi                                 1.0
sec_app_fico_range_low                 1.0
Run Code Online (Sandbox Code Playgroud)

我想了解它是如何工作的?

如果我们使用,df.isnull()只有它会为每个单元格返回 True 或 False。如何mean()给我们正确的输出。我的目标是在所有列中找到空值的百分比。以上输出代表 inq_fi 和 sec_app_fico_range_low 具有所有缺失值。

我们也没有通过 sort_values 吗?

zip*_*ipa 8

细分如下:

df.isnull()
#Mask all values that are NaN as True
df.isnull().mean()
#compute the mean of Boolean mask (True evaluates as 1 and False as 0)
df.isnull().mean().sort_values(ascending = False)
#sort the resulting series by column names descending
Run Code Online (Sandbox Code Playgroud)

这就是说具有值的列:

[np.nan, 2, 3, 4]
Run Code Online (Sandbox Code Playgroud)

被评估为:

[True, False, False, False]
Run Code Online (Sandbox Code Playgroud)

解释为:

[1, 0, 0, 0]
Run Code Online (Sandbox Code Playgroud)

导致:

0.25
Run Code Online (Sandbox Code Playgroud)