pandas.DataFrame.count#

DataFrame.count(axis=0, numeric_only=False)[源代码]#

计算每列或每行的非 NA 单元格数。

NoneNaNNaTpandas.NA 被视为 NA。

Parameters:
axis{0 或 ‘index’, 1 或 ‘columns’}, default 0

如果 axis 为 0 或 ‘index’,则为每列生成计数。如果 axis 为 1 或 ‘columns’,则为每行生成计数。

numeric_onlybool,默认 False

仅包含 float, intboolean 数据。

Returns:
Series

每列/每行的非 NA/null 条目数。

参见

Series.count

Series 中非 NA 元素的数量。

DataFrame.value_counts

计算列的唯一组合的数量。

DataFrame.shape

DataFrame 的行数和列数(包括 NA 元素)。

DataFrame.isna

显示 NA 元素位置的相同大小的布尔 DataFrame。

Examples

使用字典构造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