大数据知识体系
首页
数据结构与算法
  • JVM
  • Java
  • Scala
  • Python
设计模式
  • MySQL
  • Redis
  • HDFS
  • HBase
  • ClickHouse
  • ElasticSearch
  • Iceberg
  • Hudi
  • Spark
  • Flink
  • Hive
  • Yarn
  • Zookeeper
  • Maven
  • Git
  • 数据仓库
  • 用户画像
  • 指标体系
数据治理
关于
首页
数据结构与算法
  • JVM
  • Java
  • Scala
  • Python
设计模式
  • MySQL
  • Redis
  • HDFS
  • HBase
  • ClickHouse
  • ElasticSearch
  • Iceberg
  • Hudi
  • Spark
  • Flink
  • Hive
  • Yarn
  • Zookeeper
  • Maven
  • Git
  • 数据仓库
  • 用户画像
  • 指标体系
数据治理
关于
  • JVM

    • JVM 架构
    • JVM 类加载机制
    • JVM 运行时数据区
    • JVM 垃圾回收机制
    • JVM 性能监控与调优
    • JVM 常见错误
  • Java

    • Java基础

      • Java 基础知识
      • Java 基础 - 枚举类
      • Java 基础 - 异常处理
      • Java 基础 - 泛型
      • Java 基础 - 反射
      • Java 基础 - 代理模式
      • Java 基础 - 注解
    • Java集合

    • Java并发编程

    • Java 开发规范
  • Scala

    • Scala 概述
  • Python

    • Numpy

      • 初识 Numpy
      • ndarray 的创建方式
      • NumPy 的数据类型
      • NumPy 数组计算
      • 拷贝
      • 索引和切片
      • 数学和统计方法
      • 数组形状变换
      • 通用函数
      • 排序
      • 搜索和计数
      • 线性代数
      • 伪随机数生成
      • 广播
      • 文件输入和输出
    • Pandas

      • 初识 Pandas
      • 认识 Series 和 DataFrame
      • Series 和 DataFrame 增删改查
      • Index对象增删改查
      • 普通列和行Index相互转化
      • 快速查看整体信息
      • 数值运算
      • 合并数据集
      • 数值统计与聚合
        • 数值型统计运算
          • 一元统计
          • .sum()
          • .mean(), .std(), .var()
          • .max(), .min(), .median(), idmax(), idmin()
          • 二元统计
          • .cov()
          • .corr()
          • .corrwith()
        • 类型型统计运算
          • value_counts()
          • .count()
      • 分组聚合
      • 分类类型
      • 排序和排名
      • 时间序列
      • 文件输入与输出
      • 缺失值处理
      • 字符串处理
      • pandas sql
      • 其它
  • 语言基础
  • Python
  • Pandas
Will
2022-08-04
目录

数值统计与聚合

# 数值型统计运算

这些统计操作只对元素类型为数值型的对象有效。

相关方法汇总:

方法说明
sum总和
mean平均数
median算术中位数(50%分位数)
min、max最小值和最大值
idxmin、idxmax最小值和最大值的索引值
argmin、argmax最小值和最大值的索引位置(整数)
mad根据平均值计算平均绝对离差
var方差
std标准差
count飞NAN值的数量
value_counts计算词频或频率
describe针对Series或DataFrame的列做汇总统计
skew样本值的偏度
kurt样本值的峰度
cumsum样本值的累计和
cummin、cummax样本值的累计最小值和最大值
cumprod样本值的累计积
diff计算一阶差分
pct_changepct_change

# 一元统计

# .sum()

DataFrame.sum(axis='index')

  • axis:'index'-沿列加,'columns'-沿行加
import numpy as np
import pandas as pd
1
2
df = pd.DataFrame([[1,2],[3,5]], index=['a','b'], columns = ['A','B'])
df
1
2
A B
a 1 2
b 3 5
df.sum()  # 按列加
1
A    4
B    7
dtype: int64
df.sum(axis = 'columns')  # 按行加
1
a    3
b    8
dtype: int64

# .mean(), .std(), .var()

均值、标准差、方差

# .max(), .min(), .median(), idmax(), idmin()

最大、最小、中值

df.mad(axis = 'index')
1
A    0.75
B    0.75
dtype: float64

# 二元统计

计算任意两列直接的统计量,返回以列索引为新行索引和列索引的 DataFrame

# .cov()

DataFrame.cov(min_periods=None)

  • min_periods:每一列去除 NaN 后,要求能够参与运算的最少元素个数。
df1 = pd.DataFrame([[1,2],[2,0]], columns = ['B','C'])
df1
1
2
B C
0 1 2
1 2 0
df1.cov()
1
B C
B 0.5 -1.0
C -1.0 2.0

# .corr()

相关系数

df1.corr()
1
B C
B 1.0 -1.0
C -1.0 1.0

# .corrwith()

corr 是自身列之间的关系,而这个函数可以对不同的 DataFrame 进行运算,不要要记得运算发生在同名列和同索引的行之间。

DataFrame.corrwith(other, axis=0, drop=False)

  • other:另一个 DataFrame 或 Series
  • axis:'index'或'columns'
  • drop:是否丢掉结果中的 NaN
df1 = pd.DataFrame([[1,2],[2,0],[2,3]],index = [0,1,2],columns = ['B','C'])
df1
1
2
B C
0 1 2
1 2 0
2 2 3
df
1
A B
a 1 2
b 3 5
df.corrwith(df1)  #只对 同名列 和 同名行 进行计算
1
B   NaN
A   NaN
C   NaN
dtype: float64
s = pd.Series([1,2], index = [0,1], name = 'B')
s
1
2
0    1
1    2
Name: B, dtype: int64
df
1
A B
a 1 2
b 3 5
df.corrwith(s)
1
A   NaN
B   NaN
dtype: float64

# 类型型统计运算

# value_counts()

不适合 DataFrame。

Series/Index.value_counts(normalize=False, ascending=False, bins=None)

  • normalize:True or False,计算频次或者频率比;
  • ascending:True or False,排序方式,默认降序;
  • bins:int,pd.cut 的一种快捷操作,对连续数值型效果好;
s = pd.Series([1,2,1,2,1,3])
s
1
2
0    1
1    2
2    1
3    2
4    1
5    3
dtype: int64
s.value_counts()
1
1    3
2    2
3    1
dtype: int64
s.value_counts(ascending = True)
1
3    1
2    2
1    3
dtype: int64
s.value_counts( bins = 2)   # bins按照int平均分割,左开右闭,左侧外延1%以包含最左值
1
(0.997, 2.0]    5
(2.0, 3.0]      1
dtype: int64

# .count()

计算统计每一类 non-NaN 元素个数,这个函数可以快速了解哪些特征或哪些样本缺失比较严重。

DataFrame.count(axis=0)

  • axis: 0-查看列,1-查看行;
df
1
A B
a 1 2
b 3 5
df.count(axis = 0)
1
A    2
B    2
dtype: int64
type(df.count(axis = 1))
1
pandas.core.series.Series
上次更新: 2023/11/01, 03:11:44

← 合并数据集 分组聚合→

Theme by Vdoing | Copyright © 2022-2023 Will 蜀ICP备2022002285号-1
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式