达到最大值后,Python Pandas cumsum()重置

编程入门 行业动态 更新时间:2024-10-09 11:27:59
本文介绍了达到最大值后,Python Pandas cumsum()重置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个带有timedelta的pandas DataFrame,它是在单独的列中以毫秒表示的这些delta的累积和.下面提供了一个示例:

I have a pandas DataFrame with timedeltas as a cumulative sum of those deltas in a separate column expressed in milliseconds. An example is provided below:

Transaction_ID Time TimeDelta CumSum[ms] 1 00:00:04.500 00:00:00.000 000 2 00:00:04.600 00:00:00.100 100 3 00:00:04.762 00:00:00.162 262 4 00:00:05.543 00:00:00.781 1043 5 00:00:09.567 00:00:04.024 5067 6 00:00:10.654 00:00:01.087 6154 7 00:00:14.300 00:00:03.646 9800 8 00:00:14.532 00:00:00.232 10032 9 00:00:16.500 00:00:01.968 12000 10 00:00:17.543 00:00:01.043 13043

我希望能够提供CumSum [ms]的最大值,之后,累积总和将再次从0开始.例如,如果在上面的示例中最大值为3000,则结果看起来像所以:

I would like to be able to provide a maximum value for CumSum[ms] after which the cumulative sum would start over again at 0. For example, if the maximum value was 3000 in the above example, the results would look like so:

Transaction_ID Time TimeDelta CumSum[ms] 1 00:00:04.500 00:00:00.000 000 2 00:00:04.600 00:00:00.100 100 3 00:00:04.762 00:00:00.162 262 4 00:00:05.543 00:00:00.781 1043 5 00:00:09.567 00:00:04.024 0 6 00:00:10.654 00:00:01.087 1087 7 00:00:14.300 00:00:03.646 0 8 00:00:14.532 00:00:00.232 232 9 00:00:16.500 00:00:01.968 2200 10 00:00:17.543 00:00:01.043 0

我已经使用模运算符进行了探索,但是只有当结果总和等于所提供的限制(即cumsum [ms]为500%500等于零)时,才能成功重置为零.

I have explored using the modulo operator, but am only successful in resetting back to zero when the resulting cumsum is equal to the limit provided (i.e. cumsum[ms] of 500 % 500 equals zero).

提前感谢您的任何想法,如果可以提供更多信息,请告诉我.

Thanks in advance for any thoughts you may have, and please let me know if I can provide any more information.

推荐答案

下面是一个示例,该示例说明了如何遍历数据帧中的每一行.为了简单起见,我为该示例创建了新数据:

Here's an example of how you might do this by iterating over each row in the dataframe. I created new data for the example for simplicity:

df = pd.DataFrame({'TimeDelta': np.random.normal( 900, 60, size=100)}) print df.head() TimeDelta 0 971.021295 1 734.359861 2 867.000397 3 992.166539 4 853.281131

所以让我们用您希望的最大3000进行累加器循环:

So let's do an accumulator loop with your desired 3000 max:

maxvalue = 3000 lastvalue = 0 newcum = [] for row in df.iterrows(): thisvalue = row[1]['TimeDelta'] + lastvalue if thisvalue > maxvalue: thisvalue = 0 newcum.append( thisvalue ) lastvalue = thisvalue

然后将newcom列表放入数据框:

Then put the newcom list into the dataframe:

df['newcum'] = newcum print df.head() TimeDelta newcum 0 801.977678 801.977678 1 893.296429 1695.274107 2 935.303566 2630.577673 3 850.719497 0.000000 4 951.554206 951.554206

更多推荐

达到最大值后,Python Pandas cumsum()重置

本文发布于:2023-10-16 14:11:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1497797.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:最大值   Python   cumsum   Pandas

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!