使用实例:分别统计Excel中蛋白质和固形物数据
1、建立根据任务要求分别统计蛋白质和固形物的自定义函数(方法)
def prod_describe(data, classify, category, remove_col): desc = data.groupby([classify])[category].describe() desc.drop(columns=remove_col, axis=1, inplace=True) # Range(极差) = max - min desc["极差"] = desc["max"] - desc["min"] # 更新统计数据的列名称(英文--》中文) desc = desc.rename(columns={"count": "样品数量", "mean": "平均值", "std": "标准偏差", "min": "最小值", "max": "值"}) desc = desc.sort_values(by=["样品数量"], axis=0, ascending=False) return desc # 由于报告不需要分位数的统计量,故删除这些字段[25%, 50%, 70%] remove_col = ["25%", "50%", "75%"]
2、关键参数的赋值
classify = "产品" category1 = "蛋白质" category2 = "固形物" data = data_prep.copy()
3、实现根据产品名称分别对蛋白质和固形物进行统计
category1_desc = prod_describe(data, classify, category1, remove_col) category2_desc = prod_describe(data, classify, category2, remove_col)
4、将两种统计结果汇总在一起,写入Excel文件中并保存
category2]) file = "d:/test/Summary/Data_Statistics.xlsx" result.to_excel(file, sheet_name="Statistics") print("已经全部完成,请检查!")