-1

Say I have a pandas data frame like the following:

test = pd.DataFrame({'name':['John', 'David', 'John', 'Bob', 'Bob', 'Tim'], 'count' : [4, 5, 3, 2, 2, 1]})

How can I make a new data frame which merges the count values (adds) based on the values in the name column?

The final result for this example should be:

   count   name
0      7   John
1      5  David
2      4    Bob
3      1    Tim
aydow
  • 3,673
  • 2
  • 23
  • 40
Jack Arnestad
  • 1,845
  • 13
  • 26

1 Answers1

1

Use groupby and sum() on that

In [276]: test.groupby('name').sum().reset_index()
Out[276]:
    name  count
0    Bob      4
1  David      5
2   John      7
3    Tim      1
aydow
  • 3,673
  • 2
  • 23
  • 40