Home > Net >  Python DataFrame how to get all rows with the same name and add the values together
Python DataFrame how to get all rows with the same name and add the values together

Time:12-02

When I search for my problem all I keep finding is grouping the same values together but I am looking to get the value of each team and add them together

Eg this is my Dataframe
0   Liverpool        4        
1   West Ham         0
2   Bournemouth      1
3   Burnley          3
4   Crystal Palace   0
5   Liverpool        6
6   West Ham         2
7   Bournemouth      8
8   Burnley          1
9   Crystal Palace   4

All the examples I see online is just grouping them together

eg

0   Liverpool        4 
1   Liverpool        5       
2   West Ham         2
3   West Ham         1
4   Crystal Palace   4
5   Crystal Palace   1

but what I am after is in order of high to low

0   Liverpool        9 
1   Crystal Palace   5
2   West Ham         3

enter image description here

CodePudding user response:

From what you getting from by grouping and summing, the results almost surely tells that you have col FTHG as string, since sum() operation appends strings to other strings, you get string concat at the end rather than summed value. Try following:

match_data1["FTHG"] = match_data1.astype(int)
match_data1.groupby("HomeTeam")["FTHG"].sum().sort_values(ascending=False)

EDIT: After @Emi OB's comment. If column "FTHG" is nullable, then use float conversion, and fill na before sum (or ignore them afterwards), you can also use nansum approach which is discussed here.

match_data1["FTHG"] = match_data1.astype(float)
match_data1.groupby("HomeTeam")["FTHG"].fillna(0.0).sum().sort_values(ascending=False)
  • Related