当前位置: 首页 > article >正文

详解 Pandas 的累计统计函数

Pandas 中常用的累计统计函数有 cumsum()cummax()cummin()cumprod(),分别是用来统计 DataFrame 中按行或按列的累加值、累计最大值、累计最小值、累乘值。

一、数据准备

import pandas as pd

df = pd.DataFrame(data={
    'Product_A': [100, 300, 200, 250, 300],
    'Product_B': [50, 100, 350, 200, 250]
}, index=['1月', '2月', '3月', '4月', '5月'])

print(df)
     Product_A  Product_B
1月        100         50
2月        300        100
3月        200        350
4月        250        200
5月        300        250

二、cumsum 函数

统计从第一行(列)到当前行(列)的累加,类似于 SQL 中的 sum() over() 窗口函数

# 1.沿行轴统计累加
print(df.cumsum())  # df.cumsum(axis=0)
   Product_A  Product_B
1月        100         50
2月        400        150
3月        600        500
4月        850        700
5月       1150        950
# 2.沿列轴统计累加
print(df.cumsum(axis=1))
    Product_A  Product_B
1月        100        150
2月        300        400
3月        200        550
4月        250        450
5月        300        550

三、cummax 函数

统计从第一行(列)到当前行(列)中的最大值,类似于 SQL 中的 max() over() 窗口函数

# 1.沿行轴统计累计最大值
print(df.cummax())
    Product_A  Product_B
1月        100         50
2月        300        100
3月        300        350
4月        300        350
5月        300        350
# 2.沿列轴统计累计最大值
print(df.cummax(axis=1))
    Product_A  Product_B
1月        100        100
2月        300        300
3月        200        350
4月        250        250
5月        300        300

四、cummin 函数

统计从第一行(列)到当前行(列)中的最小值,类似于 SQL 中的 min() over() 窗口函数

# 1.沿行轴统计累计最小值
print(df.cummin())
     Product_A  Product_B
1月        100         50
2月        100         50
3月        100         50
4月        100         50
5月        100         50
# 2.沿列轴统计累计最小值
print(df.cummin(axis=1))
      Product_A  Product_B
1月        100         50
2月        300        100
3月        200        200
4月        250        200
5月        300        250

五、cumprod 函数

统计从第一行(列)到当前行(列)的累乘值

# 1.沿行轴统计累乘值
print(df.cumprod())
       Product_A    Product_B
1月           100           50
2月         30000         5000
3月       6000000      1750000
4月    1500000000    350000000
5月  450000000000  87500000000
# 2.沿列轴统计累乘值
print(df.cumprod(axis=1))
     Product_A  Product_B
1月        100       5000
2月        300      30000
3月        200      70000
4月        250      50000
5月        300      75000

http://www.kler.cn/news/306590.html

相关文章:

  • Java后端开发(十七)-- Java中对Object、Collection集合、Map集合、数组进行判空
  • 数据结构:时间复杂度与空间复杂度
  • 【代码随想录训练营第42期 Day57打卡 - 图论Part7 - Prim算法与Kruskal算法
  • 后端开发刷题 | 数字字符串转化成IP地址
  • 状态机按键消抖(学习笔记)
  • Flink有界流实现(1)
  • 【Python】谷歌浏览器总是自动更新,使用selenium跟chromedriver版本不匹配怎么办?
  • 01,大数据总结,zookeeper
  • 算法练习题27——疫情下的电影院(模拟)
  • AI辅助癌症诊断取得了进展
  • Angular面试题一
  • 大模型 LLM(Large Language Models)如今十分火爆,对于初入此领域的新人小白来说,应该如何入门 LLM 呢?是否有值得推荐的入门教程呢?
  • 深度学习自编码器 - 引言篇
  • java基于PDF底层内容流的解析对文本内容进行编辑
  • 象过河手机进销存,外出办公更方便,随时了解经营情况
  • C# 静态static
  • 基于HTML5的下拉刷新效果
  • 如何避免长距离遗忘问题
  • HarmonyOS NEXT 封装实现好用的网络模块(基于最新5.0的API12)
  • Android 12 Launcher3 去掉Hotseat
  • JVM 调优篇7 调优案例3- gc overhead limit exceed
  • ListBox显示最新数据、左移和右移操作
  • K8s中HPA自动扩缩容及hml
  • idea2024.2永久使用
  • MFC工控项目实例之十五定时刷新PC6325A模拟量输入
  • HTML添加文字
  • 【深度学习】Pytorch基础
  • 分享一些成功的 SQL 优化案例
  • 2024工业机器视觉产业现状
  • 多模态大语言模型综述(中)-算法实用指南