简体   繁体   English

Pandas:分组依据和条件总和并添加回数据帧

[英]Pandas: Group By and Conditional Sum and Add Back to Data Frame

I have a data frame like below:我有一个如下数据框:

ID  Num  Letter  Count
1   17   D       1
1   12   D       2
1   13   D       3
2   17   D       4
2   12   A       5
2   16   D       1
3   16   D       1

The objective is to sum 'Count' value for each 'ID' when 'Num' is (17 or 12) and 'Letter' is 'D', and also add the calculation back to the original data frame in 'Total'.目标是在“Num”为(17 或 12)且“Letter”为“D”时对每个“ID”的“Count”值求和,并将计算结果添加回“Total”中的原始数据框。

Below is expected data frame:以下是预期的数据框:

ID  Num  Letter  Count Total
1   17   D       1     3   
1   12   D       2     3   
1   13   D       3     3 
2   17   D       4     4 
2   12   A       5     4 
2   16   D       1     4
3   16   D       1     0

Thanks in advance!提前致谢!

Idea is replace non matched values to 0 in Series.where and then is used GroupBy.transform with sum :想法是将 Series.where 中的不匹配值替换为0 ,然后将Series.wheresum GroupBy.transform使用:

mask = df['Num'].isin([17,12]) & df['Letter'].eq('D')
df['Total'] = df['Count'].where(mask, 0).groupby(df['ID']).transform('sum')
print (df)
   ID  Num Letter  Count  Total
0   1   17      D      1      3
1   1   12      D      2      3
2   1   13      D      3      3
3   2   17      D      4      4
4   2   12      A      5      4
5   2   16      D      1      4
6   3   16      D      1      0

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

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