jll
jll

Reputation: 815

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  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. df.groupby(["id"]).agg({"c": np.sum}) is not right because the two a1 entries would have their c values summed. i want only unique values of id to contribute to the sum of c values. how can you write this in pandas?

Upvotes: 1

Views: 1245

Answers (1)

Niyas
Niyas

Reputation: 515

This should work.

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

Upvotes: 2

Related Questions