0

Consider this pandas data frame:

import pandas as pd
df = pd.DataFrame(columns=['city','pop'])
df['city'] = ['faa','foo','faa','faa','foo']
df['pop'] = [10,20,15,30,60]

  city  pop
0  faa   10
1  foo   20
2  faa   15
3  faa   30
4  foo   60

How to sum the pop of each city? I'd like to obtain

   city  pop
0   faa   55
1   foo   80
Sigur
  • 355
  • 8
  • 19
  • 1
    @ALollz, perfect. It is exactly that. I didn't know how to search for. – Sigur Oct 25 '18 at 16:48
  • 2
    Reading the Pandas docs should point you in the right direction. For your application you want to use groupby. e.g. popCount = df.groupby(by='city', as_index=False).sum() – tnknepp Oct 25 '18 at 16:49

0 Answers0