zoukankan      html  css  js  c++  java
  • 【数据分析&数据挖掘】pandas的统计分析

     1 # 在numpy 里面有统计分析, 对数值型数据进行统计指标
     2 # np.max  np.min  np.mean np.std‘
     3 
     4 import pandas  as pd
     5 import numpy as np
     6 
     7 # 1、加载数据
     8 detail = pd.read_excel("../day05/meal_order_detail.xlsx")
     9 # print("detail :
    ", detail)
    10 print("detail 的类型:
    ", type(detail))
    11 print("detail 列索引名称:
    ", detail.columns)
    12 print("detail 的元素的数据类型:
    ", detail.dtypes)
    13 
    14 # 在pandas里面对数值型数据进行统计分析
    15 # 获取 amounts counts 的统计指标
    16 print("获取最大值:
    ",detail.loc[:,["amounts","counts"]].max())
    17 print("获取最小值:
    ",detail.loc[:,["amounts","counts"]].min())
    18 print("获取均值:
    ",detail.loc[:,["amounts","counts"]].mean())
    19 print("获取中位数:
    ",detail.loc[:,["amounts","counts"]].median())
    20 print("获取标准差:
    ",detail.loc[:,["amounts","counts"]].std())
    21 print("获取方差:
    ",detail.loc[:,["amounts","counts"]].var())
    22 print("获取非空数据的数量:
    ",detail.loc[:,["amounts","counts"]].count())
    23 print("获取最大值的位置:
    ",detail.loc[:,["amounts","counts"]].idxmax())  # np.argmax()
    24 print("获取最小值的位置:
    ",detail.loc[:,["amounts","counts"]].idxmin()) # np.argmin()
    25 
    26 
    27 # 返回一个众数的dataframe
    28 res = detail.loc[:,["amounts","counts"]].mode()
    29 print("res: 
    ", res)
    30 print("获取amounts众数
    ",res["amounts"])
    31 print("获取counts众数
    ",res["counts"])
    32 print("获取众数的类型
    ",type(res))
    33 # 返回一个 众数的 series
    34 print("获取众数
    ",detail.loc[:,"amounts"].mode())
    35 
    36 # 获取分位数
    37 # 默认获取 50% 的分位数---即 中位数
    38 print("获取分位数:
    ", detail.loc[:, ["amounts", "counts"]].quantile())
    39 # # 获取四分位数 --通过给q 传参来获取四分位数
    40 print("获取分位数:
    ", detail.loc[:, ["amounts", "counts"]].quantile(q=np.arange(0, 1 + 0.25, 0.25)))
    41 
    42 
    43 # 获取describe 统计分析
    44 # 返回8中结果
    45 # 非空数量
    46 # 均值
    47 # 标准差
    48 # 最小值
    49 # 分位数
    50 # 最大值
    51 print("获取describe 统计分析:
    ", detail.loc[:, ["amounts", "counts"]].describe())
    52 
    53 # 也可以对非数值型数据进行统计分析 ---使用describe对于非数值型数据进行统计分析
    54 # 返回4种结果
    55 # 非空数据的数量
    56 # 去重之后的数据数量
    57 # 众数
    58 # 众数出现的次数
    59 # 在统计分析之前将数据转化为类别型数据---category
    60 # 可以使用astype 来修改数据类型
    61 detail.loc[:, "dishes_name"] = detail.loc[:, "dishes_name"].astype("category")
    62 # #
    63 # # #
    64 print("修改数据类型之后的detail :
    ", detail.dtypes)
    65 # #
    66 print("获取非数值型数据的describe 统计分析:
    ", detail.loc[:, "dishes_name"].describe())
    67 
    68 # 1、利用统计分析,以及dataframe的删除方式,删除detail里面数据全是空的列
    69 # 2、梳理pandas的xmind
    70 # 3、掌握numpy、matplotlib、pandas 的所学操作
  • 相关阅读:
    poj 2754 Similarity of necklaces 2 转换成多重背包,单调队列优化/ 二进制优化
    new和delete2
    new和delete1
    new和delete4
    new和delete3
    new(placement new)
    用例图中的Actor(参与者)一定是人吗?
    二维数组的函数参数传递
    二维指针动态创建二维数组(C/C++)
    OOD的五项基本原则——SOLID
  • 原文地址:https://www.cnblogs.com/Tree0108/p/12116029.html
Copyright © 2011-2022 走看看