DataFrame.count(axis=0, level=None, numeric_only=False)
计算每列或每行的非NA单元格。
值None
,NaN
,NaT
和可选的numpy.inf
(取决于pandas.options.mode.use_inf_as_na
)被视为NA
。
参数: | axis : {0 or ‘index’, 1 or ‘columns’}, 默认为 0 如果为每列生成0或'index'计数。 如果为每行生成1或'columns'计数 level : int 或 str, 可选 如果轴是MultiIndex(分层), 则沿特定级别计数,折叠到DataFrame中。 一个str指定级别名称。 numeric_only : boolean, 默认为 False 仅包含float,int或boolean数据。 |
返回: |
对于每个列/行,non-NA/null条目的数量。 如果指定了level,则返回 |
例子
从字典构造DataFrame:
>>> df = pd.DataFrame({"Person":
... ["John", "Myla", "Lewis", "John", "Myla"],
... "Age": [24., np.nan, 21., 33, 26],
... "Single": [False, True, True, True, False]})
>>> df
Person Age Single
0 John 24.0 False
1 Myla NaN True
2 Lewis 21.0 True
3 John 33.0 True
4 Myla 26.0 False
注意不计数的NA值:
>>> df.count()
Person 5
Age 4
Single 5
dtype: int64
每行计数:
>>> df.count(axis='columns')
0 3
1 2
2 3
3 3
4 3
dtype: int64
计算MultiIndex的一个级别:
>>> df.set_index(["Person", "Single"]).count(level="Person")
Age
Person
John 2
Lewis 1
Myla 1