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

Time:11-21

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.

CodePudding user response:

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
  • Related