当我执行df.isnull()。sum()时,我得到一列中空值的计数。但是.sum()的默认轴为None或0-应该在各列中求和。
当默认值表示跨轴求和= 0时,为什么.sum()计算列而不是行的和?
谢谢!
我看到了与您所解释的相反的行为:
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
汇总各列
In [3310]: df1.isnull().sum()
Out[3310]:
date 0
variable 1
value 0
dtype: int64
,
轴参数与您希望求和的方向正交。
不幸的是,sum 的 Pandas 文档目前没有明确说明这一点,但 count 的文档确实如此: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.count.html
参数 轴{0 或‘索引’,1 或‘列’},默认为 0 如果为每列生成 0 或“索引”计数。如果为每行生成 1 或“列”计数。
,嗯..这不是我所看到的功能。让我们来看这个小例子。
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”,索引或行索引是小写。
输出:
A B
a NaN 1
b NaN 1
c 3.0 3
A 2
B 0
dtype: int64
A 3.0
B 5.0
dtype: float64
每docs:
axis:{索引(0),列(1)}要应用的功能的轴 上。