pandas的统计指标
加载数据
import pandas as pd
data = pd.read_excel('day06/data/meal_order_detail.xls', sheet_name=0)
print('data:\n', data)
print('columns:\n', data.columns)
# 查看数据类型
print('dtypes:\n', data.dtypes)
统计数据
常用数据
print('统计最大值:\n', data.loc[:, 'amounts'].max())
print('统计最小值:\n', data.loc[:, 'amounts'].min())
print('统计均值:\n', data.loc[:, 'amounts'].mean())
print('统计中位数:\n', data.loc[:, 'amounts'].median())
print('最大值位置:\n', data.loc[:, 'amounts'].idxmax())
print('最小值位置:\n', data.loc[:, 'amounts'].idxmin())
print('统计方差:\n', data.loc[:, 'amounts'].var())
print('统计标准差:\n', data.loc[:, 'amounts'].std())
统计的非空数据的数量(count)
空数据
为用 NaN 占位的数据
print('统计count:\n', data.loc[:, 'amounts'].count())
print('统计 cost的count:\n', data.loc[:, 'cost'].count())
分位数(quantile)
quantile
方法有一个q
参数,用0~1
指定要取出的分位数,可使用数组取出多个数,默认为0.5,即中位数。
print('统计quantile:\n', data.loc[:, 'amounts'].quantile()
求四分位数
四分位数即:最小值、下四分位数、中位数、上四分位数、最大值 ——
5个数
将数据分为4部分
print('统计quantile:\n', data.loc[:, 'amounts'].quantile(q=[0, 0.25, 0.5, 0.75, 1]))
众数(mode)
众数可能有多个,返回值为Series结构。
print('统计众数:\n', data.loc[:, 'amounts'].mode())
0
是行索引35
是众数的值
统计描述(describe)
使用describe方法返回8种结果,分别为:
- 非空数据的数目
- 均值
- 标准差
- 四分位数(最小值、下四分位数、中位数、上四分位数、最大值)
print('统计描述:\n', data.loc[:, 'amounts'].describe())
对非数值型数据的统计指标
print('统计众数:\n', data.loc[:, 'dishes_name'].mode())
# 返回4种结果:非空数据的数目、去重之后所剩下的类别的数目、众数、众数出现的次数
print('统计描述:\n', data.loc[:, 'dishes_name'].describe())
对非数值型使用
统计描述
将返回4种结果:
- 非空数据的数目
- 去重之后所剩下的类别的数目
- 众数
- 众数出现的次数
转化为 类别型(category–非数值的)
# 可以将指定的列转化为 类别型(category---非数值的)
data.loc[:, 'dishes_name'] = data.loc[:, 'dishes_name'].astype('category')
print('data:\n', data.dtypes)
print('统计众数:\n', data.loc[:, 'dishes_name'].mode())
print('统计描述:\n', data.loc[:, 'dishes_name'].describe())