简体   繁体   中英

How to sum rows of a column with same key in a different column of the dataframe?

  df = prem[['HomeTeam','HP','AP']]
  print(df)
             HomeTeam  HP  AP
  0         Liverpool   3   0
  1          West Ham   0   3
  2       Bournemouth   1   1
  3           Burnley   3   0
  4    Crystal Palace   1   1
  ..              ...  ..  ..
  283          Wolves   1   1
  284         Burnley   1   1
  285         Chelsea   3   0
  286      Man United   3   0
  287       Leicester   3   0     

I am wondering how I would sum all of the home points for West Ham. All I can come up with is the sum of the whole HP, home points, column.

From what is being asked, i would guess then it is:

df.loc[df['HomeTeam'] == 'West Ham'], 'HP'].sum()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM