Pandas DataFrame count() 方法
示例
计算每行(非 NULL 值)的数量
import pandas as pd
data = {
"Duration": [50, 40, None, None, 90, 20],
"Pulse": [109, 140, 110, 125, 138, 170]
}
df = pd.DataFrame(data)
print(df.count())
自己动手试一试 »
定义和用法
count()
方法计算每行(或如果指定 axis 参数为 axis='columns'
,则为每列)的非空值数量,并返回一个 Series 对象,其中包含每行(或每列)的结果。
语法
dataframe.count(axis, level, numeric_only)
参数
axis
、level
和 numeric_only
参数是 关键字参数。
参数 | 值 | 描述 |
---|---|---|
axis | 0 |
可选,要检查的轴,默认为 0。 |
level | Number level name |
可选,指定沿哪个级别(在分层多索引中)进行计数 |
numeric_only | 'True' |
可选,默认为 False,如果 count 方法应该只计算数值,则设置为 true |
返回值
一个 Series 对象,包含每行/列的计数结果。
如果指定了 level 参数,则此方法将返回一个DataFrame 对象。
此函数不会修改原始 DataFrame 对象。