DataFrame中的count()函数,以及常用的统计方法

目录

count()函数

pandas.DataFrame.count

DataFrame中常见的其他方法:

分组统计


count()函数

官方API为:

pandas.DataFrame.count

DataFrame.count(axis=0level=Nonenumeric_only=False)[source]

Count non-NA cells for each column or row.

The values None, NaN, NaT, and optionally numpy.inf (depending on pandas.options.mode.use_inf_as_na) are considered NA.

Parameters:

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

If 0 or ‘index’ counts are generated for each column. If 1 or ‘columns’ counts are generated for each row.

level : int or str, optional

If the axis is a MultiIndex (hierarchical), count along a particular level, collapsing into a DataFrame. A str specifies the level name.

numeric_only : boolean, default False

Include only float, int or boolean data.

Returns:

Series or DataFrame

For each column/row the number of non-NA/null entries. If level is specified returns a DataFrame.

See also

Series.count

Number of non-NA elements in a Series.

DataFrame.shape

Number of DataFrame rows and columns (including NA elements).

DataFrame.isna

Boolean same-sized DataFrame showing places of NA elements.

*****************************************翻译一下******************************************

pandas.DataFrame.count

DataFrame。计数(轴= 0,水平= None, numeric_only = False)[源]

计算每一列或每一行的非na细胞。

值None、NaN、NaT和可选的numpy。inf(取决于pandas.options.mode.use_inf_as_na)被认为是NA。

参数:

轴:{0或' index ', 1或' columns '},默认为0

如果为每个列生成0或' index '计数。如果为每一行生成1个或“列”计数。

级别:int或str,可选

如果轴是一个多索引(层次结构),则沿着特定的级别计数,折叠成一个数据aframe。str指定级别名称。

numeric_only:布尔值,默认为False

只包含浮点数、int或boolean数据。

返回:

系列或DataFrame

对于每一列/行,非na /null项的数量。如果指定level,则返回一个DataFrame。

另请参阅

Series.count

一个数列中非na元素的个数。

DataFrame.shape

数据aframe行和列的数量(包括NA元素)。

DataFrame.isna

布尔相同大小的数据aframe显示NA元素的位置。

******************************************给出的例子****************************************************

1、

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

2、统计NA

>>> df.count()
Person    5
Age       4
Single    5
dtype: int64

3、针对每一行,进行统计

df.count(axis='columns')
0    3
1    2
2    3
3    3
4    3
dtype: int64

注意:这里axis='columns'表示按“列”操作,相当于axis=0;如果axis=1,对每一行进行操作

4、计算多索引的一个级别

>>> df.set_index(["Person", "Single"]).count(level="Person")
        Age
Person
John      2
Lewis     1
Myla      1

DataFrame中常见的其他方法:

df.count() #非空元素计算 
df.min() #最小值 
df.max() #最大值 
df.idxmin() #最小值的位置,类似于R中的which.min函数 
df.idxmax() #最大值的位置,类似于R中的which.max函数 
df.quantile(0.1) #10%分位数 
df.sum() #求和 
df.mean() #均值 
df.median() #中位数 
df.mode() #众数 
df.var() #方差 
df.std() #标准差 
df.mad() #平均绝对偏差 
df.skew() #偏度 
df.kurt() #峰度 
df.describe() #一次性输出多个描述性统计指标

分组统计

df.groupby('Person').sum()

 

 

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值