0

How to caunt all values from col2 based on col1 name with Pandas?

Input:

col1     col2
-------------
one      1
two      2
three    1
one      4
two      4
three    2

Output:

col1     col2
-------------
one      5
two      6
three    3
Dmiich
  • 325
  • 2
  • 16

1 Answers1

1

Use GroupBy.sum

df.groupby('col1', as_index=False)['col2'].sum()
# df.set_index('col1').sum(level='col1').reset_index() #decrepated in future

    col1  col2
0    one     5
1  three     3
2    two     6
ansev
  • 30,322
  • 5
  • 17
  • 31