DataFrame常用方法:
基本数学操作
复杂功能:分组统计
pandas.DataFrame.count
功能
参数
相关补充
给出的例子
set_index相关补充
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()
pandas.DataFrame.count
功能
计数
参数
轴:{0或 index ,1或 columns默认为0
若每列生成0或 index 计数。若为每行生成一个或列计数。
2、级别:int或str,可选
如果轴是多索引层次结构),则沿特定的层次计数折叠成一个dataframe。str指定级别名称。
3、numeric_only:布尔值,默认为False
只包括浮点数,int或boolean数据。
相关补充
- Series.count:中非数列na元素的数量。
- DataFrame.shape:dataframe行和列的数量(包括NA元素)。
- DataFrame.isna:大小相同的布尔dataframe显示NA元素的位置。
给出的例子
1、构建一个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
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
set_index相关补充
DataFrame可以通过set_index该方法,可用设置单索引和复合索引
DataFrame.set_index(keys,drop=True,append=False,inplace=False,verify_integrity=False)
参数:
- keys:label or array-like or list of labels/arrays,这是一个需要设置为索引的列,可以是单个列或多个列
- drop:bool,default True,删除列应用作新索引
- append:bool,default False,添加新索引
- inplace:bool,default False,是否要覆盖数据集
- verify_integrity:bool,default False,检查新索引是否重复。否则,将检查推迟到必要时。False提高这种方法的性能
官网例子:
df = pd.DataFrame({'month 'year': [2012, 2014, 2013, 2014], 'sale': [55, 40, 84, 31]})#设置单个列作为索引df.set_index('month')''' year salemonth1 2012 554 2014 407 2013 8410 2014 31'''#设置复合索引df.set_index(['year', 'month'])''' saleyear month2012 1 552014 4 402013 7 842014 10 31'''#自定义索引和某列作为复合索引df.set_index([pd.Index([1, 2, 3, 4]), 'year'])''' month sale year1 2012 1 552 2014 4 403 2013 7 844 2014 10 31'''#自定义索引s = pd.Series([1, 2, 3, 4])df.set_index([s, s**2])''' month year sale1 1 1 2012 552 4 4 2014 403 9 7 2013 844 16 10 2014 31'''