pandas中cummax() 函数的应用

cummax() 函数用于计算 DataFrame 或 Series 中数值型数据的累积最大值。它将沿着指定的轴(行或列)对数据进行累积求最大值,并返回一个具有相同形状的 DataFrame 或 Series。

下面是一个示例,说明如何使用 cummax() 函数:

import pandas as pd

# 创建一个 DataFrame
data = {
    'A': [1, 2, 3, 4, 5],
    'B': [4, 5, 2, 7, 8],
    'C': [7, 3, 9, 10, 11]
}
df = pd.DataFrame(data)

# 计算整个 DataFrame 的累积最大值
total_cummax = df.cummax()
print("Total cumulative maximum of DataFrame:")
print(total_cummax)

# 计算每列的累积最大值
column_cummax = df.cummax(axis=0)
print("\nColumn cumulative maximum:")
print(column_cummax)

# 计算每行的累积最大值
row_cummax = df.cummax(axis=1)
print("\nRow cumulative maximum:")
print(row_cummax)

输出结果:

Total cumulative maximum of DataFrame:
   A  B   C
0  1  4   7
1  2  5   7
2  3  5   9
3  4  7  10
4  5  8  11

Column cumulative maximum:
   A  B   C
0  1  4   7
1  2  5   7
2  3  5   9
3  4  7  10
4  5  8  11

Row cumulative maximum:
   A  B   C
0  1  4   7
1  2  5   5
2  3  3   9
3  4  7  10
4  5  8  11

在这个示例中,我们首先创建了一个 DataFrame,并使用 cummax() 函数计算了整个 DataFrame 的累积最大值、每列的累积最大值以及每行的累积最大值。可以通过指定 axis 参数来沿着行或列进行计算累积最大值,默认情况下是对列进行计算累积最大值。

 

 

 

  • 8
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值