pandas 数据帧 - 运行总和与重置 [英] Pandas dataframe - running sum with reset

查看:114
本文介绍了 pandas 数据帧 - 运行总和与重置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想计算给定列中的运行总和(当然不使用循环)。请注意,我有另一列指定何时将运行总和重置为该行中存在的值。通过以下示例解释最好:

I want to calculate the running sum in a given column(without using loops, of course). The caveat is that I have this other column that specifies when to reset the running sum to the value present in that row. Best explained by the following example:

   reset  val   desired_col
0      0    1   1
1      0    5   6
2      0    4   10
3      1    2   2
4      1   -1   -1
5      0    6   5
6      0    4   9
7      1    2   2

desired_col 是我想要的值

推荐答案

你可以使用2次 cumsum() / p>

You can use 2 times cumsum():

#   reset  val  desired_col
#0      0    1            1
#1      0    5            6
#2      0    4           10
#3      1    2            2
#4      1   -1           -1
#5      0    6            5
#6      0    4            9
#7      1    2            2
df['cumsum'] = df['reset'].cumsum()
#cumulative sums of groups to column des
df['des']= df.groupby(['cumsum'])['val'].cumsum()
print df
#   reset  val  desired_col  cumsum  des
#0      0    1            1       0    1
#1      0    5            6       0    6
#2      0    4           10       0   10
#3      1    2            2       1    2
#4      1   -1           -1       2   -1
#5      0    6            5       2    5
#6      0    4            9       2    9
#7      1    2            2       3    2
#remove columns desired_col and cumsum
df = df.drop(['desired_col', 'cumsum'], axis=1)
print df
#   reset  val  des
#0      0    1    1
#1      0    5    6
#2      0    4   10
#3      1    2    2
#4      1   -1   -1
#5      0    6    5
#6      0    4    9
#7      1    2    2

这篇关于 pandas 数据帧 - 运行总和与重置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆