【问题标题】:Find cumulative maximum of a pandas series, but reset the cumulative maximum when 0 is encountered查找熊猫系列的累积最大值,但在遇到 0 时重置累积最大值
【发布时间】:2022-06-10 21:12:41
【问题描述】:

考虑以下系列:

  1. 100
  2. 102
  3. 101
  4. 103
  5. 0
  6. 12
  7. 123
  8. 14

我希望结果如下:

  1. 100
  2. 102
  3. 102
  4. 103
  5. 0
  6. 12
  7. 123
  8. 123

【问题讨论】:

    标签: python pandas numpy


    【解决方案1】:

    d是包含你的系列的变量,然后groupbyd == 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-10
      • 2022-12-31
      • 1970-01-01
      • 1970-01-01
      • 2020-07-24
      • 1970-01-01
      相关资源
      最近更新 更多