【问题标题】:Find cumulative maximum of a pandas series, but reset the cumulative maximum when 0 is encountered查找熊猫系列的累积最大值,但在遇到 0 时重置累积最大值 【发布时间】:2022-06-10 21:12:41 【问题描述】: 考虑以下系列: 100 102 101 103 0 12 123 14 我希望结果如下: 100 102 102 103 0 12 123 123 【问题讨论】: 标签: python pandas numpy 【解决方案1】: 设d是包含你的系列的变量,然后groupby是d == 0的累计和,然后得到cummax d.groupby(d.eq(0).cumsum()).cummax() Out[37]: 0 100 1 102 2 102 3 103 4 0 5 12 6 123 7 123 【讨论】: