pandas数值型数据和非数值型数据统计

对单列数据进行统计

加载数据

import pandas as pd

detail = pd.read_excel('./meal_order_detail.xlsx')

常见的数值统计的方法如下:

统计detail中的,单价相关指标
print('最大值',detail.loc[:,'amounts'].max())
print('最小值',detail.loc[:,'amounts'].min())
print('均值',detail.loc[:,'amounts'].mean())
print('中位数',detail.loc[:,'amounts'].median())
print('方差',detail.loc[:,'amounts'].var())
print('极差',detail.loc[:,'amounts'].ptp())
print('标准差',detail.loc[:,'amounts'].std())
print('众数',detail.loc[:,'amounts'].mode())
print('非空值的数目',detail.loc[:,'amounts'].count())
print('最大值的位置',detail.loc[:,'amounts'].idxmax())
print('最小值的位置',detail.loc[:,'amounts'].idxmin())

describe对于数值型的数据返回8中统计结果

print('describe',detail.loc[:,'amounts'].describe())

对多列数据进行统计

格式如下:

print('describe',detail.loc[:,['amounts','counts']].describe())  

简单来说,列的位置加入列名称列表即可

非数值统计统计

对于dataframe转化数据类型

其他类型转化成object,非数值型返回4个数据

detail.loc[:,'amounts'] = detail.loc[:,'amounts'].astype('object')  
print(detail.loc[:,'amounts'].describe()
print(detail.loc[:,'amounts'].dtypes)   

 

其他类型数据转化成类别型数据

detail.loc[:,'amounts'] = detail.loc[:,'amounts'].astype('category') 
print(detail.loc[:,'amounts'].describe()
print(detail.loc[:,'amounts'].dtypes)   

detail中那些菜品最火?菜品卖出多少份?

detail.loc[:,'dishes_name'] = detail.loc[:,'dishes_name'].astype('category')
print('按照deshed_name统计描述信息:',detail.loc[:,'dishes_name'].describe())        

发现这里的最火菜品是大碗白饭,但是大碗白饭不是菜品,所有重新计算。

删除数据中的大碗白饭

bool_id = detail.loc[:,'dishes_name'] == '白饭/大碗'
index = detail.loc[bool_id,:].index             
detail.drop(labels=index,axis=0,inplace=True)   

把数据类型重新转化,然后再赋给数据本身。

detail.loc[:,'dishes_name'] = detail.loc[:,'dishes_name'].astype('category')  
#  在进行统计描述信息                                                                  
print("按照dishes_name统计描述信息:\n",detail.loc[:,'dishes_name'].describe())        

在返回数据为

detail.loc[:,'dishes_name'] = detail.loc[:,'dishes_name'].astype('category')  
#  在进行统计描述信息                                                                  
print("按照dishes_name统计描述信息:\n",detail.loc[:,'dishes_name'].describe())        

在detail中哪个菜品点的最多,点了多少分菜?

将order_id转变成类别型数据,再进行describe

detail.loc[:,'order_id'] = detail.loc[:,'order_id'].astype('category')    
print('按照order_id统计描述信息为:',detail.loc[:,'order_id'].describe())           

建议在使用时,把数据类型转成category,然后再计算

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值