简体   繁体   English

根据熊猫中列的唯一分组对数据框值求和

[英]summing dataframe values based on unique grouping of column in pandas

i want to aggregate values from this pandas table after grouping by name : 我想按name分组后,从此pandas表中汇总值:

name  id  c   
john  a1  10  
john  a1  10
bob   a2  20
mary  a3  30

specifically i want to sum the values of c , grouped by name , but only for instances where id is unique. 具体来说,我想对按name分组的c的值求和,但仅适用于id唯一的实例。 df.groupby(["id"]).agg({"c": np.sum}) is not right because the two a1 entries would have their c values summed. df.groupby(["id"]).agg({"c": np.sum})是不正确的,因为两个a1条目的c值必须相加。 i want only unique values of id to contribute to the sum of c values. 我只希望id唯一值有助于c值的总和。 how can you write this in pandas? 你怎么用熊猫写这个?

This should work. 这应该工作。

df.drop_duplicates(['name', 'id'], keep='first', inplace=True)
df = df.groupby('name').sum().reset_index()

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

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