0

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.

1 Answers1

2

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
Timeless
  • 22,580
  • 4
  • 12
  • 30