How to sum values of a column where the column name has been duplicated?

Question:

I have a dataframe:

df = pd.DataFrame({'grps': list('aaabbcaabcccbbc'), 
                'vals': [12,345,-3,1,45,14,4,52,54,23,235,-21,57,-3,87]})

I want to find the sum of ‘vals’ of each group: a,b,c

I’ve tried using the .sum() function but I’m struggling on how to group all the values of the same letter.

Asked By: gallagouch

||

Answers:

You can use GroupBy.sum :

out= df.groupby("grps", as_index=False).sum()

# Output :

print(out)

  grps  vals
0    a   410
1    b   154
2    c   338
Answered By: abokey
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.