[英]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.