为什么df.isnull().sum()的工作原理是什么?

2024-04-25 00:21:13 发布

您现在位置:Python中文网/ 问答频道 /正文

当我这么做的时候df.isnull().sum(),我获取列中空值的计数。但是.sum()的默认轴是None,或0,它应该是跨列求和的。在

为什么当默认值为sum over axis=0时.sum()计算列的和而不是行?在

谢谢!在


Tags: nonedfover计数sum空值axisisnull
2条回答

我看到了你解释的相反的行为:

Sums across the columns
In [3309]:  df1.isnull().sum(1)                                                                                                                                                                
Out[3309]: 
0     0
1     1
2     0
3     0
4     0
5     0
6     0
7     0
8     0
9     0
10    0
11    0
dtype: int64

对列进行汇总

^{pr2}$

呃。。这不是我看到的功能。让我们看看这个小例子。在

import pandas as pd 
import numpy as np 

df = pd.DataFrame({'A':[np.nan, np.nan, 3],'B':[1,1,3]}, index =[*'abc'])
print(df)
print(df.isnull().sum())
print(df.sum())

注意,列是大写的“A”和“B”,索引或行索引是小写的。在

输出:

^{pr2}$

根据docs

axis : {index (0), columns (1)} Axis for the function to be applied on.

相关问题 更多 >