无涯教程-DataFrame.count函数

130 阅读1分钟

Pandascount()定义为一种用于计算每一列或每一行的非NA单元数的方法。

count - 语法

DataFrame.count(axis=0, level=None, numeric_only=False)

count - 参数

  • axis                      - {0或index,1或columns},默认值0 0或index用于行,而1或columns用于列。
  • level                     -  int或str 它是一个可选参数。如果轴是分层轴,则它会随着特定级别一起计数并折叠到DataFrame中。
  • numeric_only  - 布尔型,默认值为False 它仅包含int,float或Boolean数据。

count - 返回值

如果指定级别,则返回Series或DataFrame的计数。

示例1:下面的示例演示 count()的工作。

import pandas as pd
import numpy as np
info = pd.DataFrame({"Person":["Parker", "Learnfk", "William", "John"],
"Age": [27., 29, np.nan, 32]
info.count()

输出

Person    5
Age       3
dtype: int64

示例2:如果要对每一行进行计数,则可以使用 axis 参数。以下代码演示了 axis 参数的工作方式。

import pandas as pd
import numpy as np
info = pd.DataFrame({"Person":["Parker", "Learnfk", "William", "John"],
"Age": [27., 29, np.nan, 32]
info.count(axis=columns)

输出

0       2
1       2
2       1
3       2
dtype: int64

参考链接

www.learnfk.com/pandas/pand…