简体   繁体   English

每个组的最新成员的总和

[英]Running sum of most recent members of each group

Here is the input dataframe: 这是输入数据框:

  id  val
0  A    1
1  B    2
2  A   -3
3  C    1
4  D    5
5  B    6
6  C   -2

I would like to group entries by id, and then calculate a running sum of the most recent members of each group seen up to this point. 我想按ID对条目进行分组,然后计算到目前为止每个组中最新成员的运行总和。 Here is how the desired output would look like, with explanations how it is obtained: 这是所需输出的样子,并解释了如何获得它:

  id  val  out
0  A    1   1
1  B    2   3   (2 + 1)
2  A   -3   -1  (-3 + 2)  
3  C    1   0   (1+ -3 +2)
4  D    5   5   (5 + 1 + -3 + 2_
5  B    6   9   (6 + 5 + 1 + -3)
6  C   -2   6    (-2 + 6 + 5 -3)

Here are some more detailed explanations: 1) The row with id=1 has 3=2+1, because at that time you have 2 groups, As and Bs, each with 1 row, so you have to take that single row from each group. 下面是一些更详细的说明:1)id = 1的行具有3 = 2 + 1,因为那时您有2个组,即As和B,每个组都有1行,因此您必须从每个行中取出那一行组。

2) The row with id=2 has -1=-3+2 because at that time, you have 2 groups, As and Bs. 2)id = 2的行具有-1 = -3 + 2,因为那时您有2个组,即As和Bs。 The most recent row from the As is 2 A -3 and the single (and thus most recent) row from Bs is 1 B 2 , so you add these 2 rows. 来自As的最新行是2 A -3 ,来自Bs的单个(因此也是最新的)行是1 B 2 ,因此您将这两行相加。

3) In the row with id=6, you add up 3)在id = 6的行中,您将

2  A   -3
4  D    5
5  B    6
6  C   -2

You are taking 1 row from each group, and that is the row that is most recent at that point. 您从每个组中获取1行,这是该点最近的行。

this should be a relatively quick and easy way to do this using a loop. 这应该是使用循环的相对快速简便的方法。 the way it works is that it adds a new entry to a dictionary whenever it finds one. 它的工作方式是在找到字典时就向字典添加新条目。 if the entry already exists it overwrites the corresponding value. 如果该条目已经存在,它将覆盖相应的值。

df = pd.DataFrame({'id': ['A','B','A','C','D','B','C'],
                  'val': [1,2,-3,1,5,6,-2]})

num_rows = df.shape[0]

last_vals = {}
for i in range(0, num_rows):
    x = df['id'][i]
    last_vals[x] = df['val'][i]

sum(last_vals.values())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM