简体   繁体   English

如何根据另一列值Python总结一列

[英]How to sum up a column based on another columns value Python

I have this example df我有这个例子 df

col1 = [1,1,1,2,2,1,1,1,2,2,2]
col2 = [20, 23, 12, 44, 14, 42, 44, 1, 42, 62, 11]

data = {"col1": col1, "col2": col2}
df = pd.DataFrame(data)

I need to add a column that adds up the col2 every time the col1 is 1 and then the same for when it is 2. I have tried grouping by col1 but this skips every time there is a 2 in between The expected output would be this.我需要添加一个列,每次 col1 为 1 时将 col2 加起来,然后在 col1 为 2 时加起来。我尝试按 col1 分组,但每次之间有 2 时都会跳过预期输出将是这个.

col1    col2  col3
1       20    55 
1       23    55
1       12    55
2       44    58
2       14    58
1       42    87
1       44    87
1       1     87
2       42    115
2       62    115
2       11    115

Please let me know how to approach this请让我知道如何解决这个问题

Use GroupBy.transform with helper Series for consecutive values generated by comapre shifted values for not equal and cumulative sum:GroupBy.transform与 helper Series一起用于由不相等和累积和的comapre 移位值生成的连续值:

df['col3'] = df.groupby(df['col1'].ne(df['col1'].shift()).cumsum())['col2'].transform('sum')
print (df)
    col1  col2  col3
0      1    20    55
1      1    23    55
2      1    12    55
3      2    44    58
4      2    14    58
5      1    42    87
6      1    44    87
7      1     1    87
8      2    42   115
9      2    62   115
10     2    11   115

You can do this by creating a column that will mark every time there is a change in col1 and then sum by groupby:您可以通过创建一个列来执行此操作,该列将在 col1 每次发生更改时进行标记,然后按 groupby 求和:

i = df.col1    
df['Var3'] = i.ne(i.shift()).cumsum()
df['sums'] = df.groupby(['Var3'])['col2'].transform('sum')

which gives这使

col1  col2  Var3  sums
0      1    20     1    55
1      1    23     1    55
2      1    12     1    55
3      2    44     2    58
4      2    14     2    58
5      1    42     3    87
6      1    44     3    87
7      1     1     3    87
8      2    42     4   115
9      2    62     4   115
10     2    11     4   115
​

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

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