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

问题描述 投票:0回答:2

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

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

这是输出的一部分-

inq_fi                                 1.0
sec_app_fico_range_low                 1.0

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

如果我们使用,

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

我们也没有通过 sort_values 吗?

python python-3.x pandas
2个回答
10
投票

分解看起来像这样:

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

也就是说,有一列具有值:

[np.nan, 2, 3, 4]

评价为:

[True, False, False, False]

解释为:

[1, 0, 0, 0]

结果:

0.25

0
投票

所有的大人都曾经是孩子……但只有少数人记得……

  • 安托万·德·圣埃克苏佩里,《小王子》

df.isnull().mean() 只会返回值的平均值(“True”为 1),正如您从文档中看到的 (.mean([axis,skipna, numeric_only]) 返回值的平均值在请求的轴上。) ->

df.isnull.mean()
=
df.isnull.sum()/len(df)

参考:

  1. “缺失数据的计算”(https://pandas.pydata.org/docs/user_guide/missing_data.html#calculations-with-missing-data
  2. “描述性统计” (https://pandas.pydata.org/docs/user_guide/basics.html#basics-stats
  3. “计算/描述性统计” (https://pandas.pydata.org/docs/reference/frame.html#computations-descriptive-stats
© www.soinside.com 2019 - 2024. All rights reserved.