【问题标题】:Index of the element in the series where the cumulative some is equal to or greater than 10序列中累积 some 等于或大于 10 的元素的索引
【发布时间】:2021-09-04 00:36:06
【问题描述】:

我有这个 Panda 整数系列,例如,s = pd.Series([1, 2, 3, 1, 5, 10])。我需要找到累积总和等于或大于某个数字的元素的索引,例如10。所以,对于这个系列,因为print(s.cumsum()) 给了我们

0     1
1     3
2     6
3     7
4    12
5    22

我需要得到4,因为这里的累计总和大于10

找到该索引的最佳 Python 方法是什么?

【问题讨论】:

  • 另一种方式out=s.cumsum() 最后idx=out[out.gt(10)].index[0]

标签: pandas indexing cumsum


【解决方案1】:

只需在cumsum 之后添加条件idxmax

(s.cumsum()>10).idxmax()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-25
    • 2016-07-27
    • 1970-01-01
    • 2013-05-27
    • 2020-07-30
    相关资源
    最近更新 更多