Ben Ng
Ben Ng

Reputation: 15

Python pandas groupby sum displaying wrong output

I'm using groupby sum and I am getting the wrong output:

This is my dataframe

Although the medal column only contains value of either 0 or 1, I am getting this output after executing the following code.

test=oly_new.groupby(['Country','Year'])['Medal'].sum()

Upvotes: 1

Views: 412

Answers (1)

EdChum
EdChum

Reputation: 393973

Your Medal column is a str, convert first to int and then sum:

oly_new['Medal'] = oly_new['Medal'].astype(int)
test=oly_new.groupby(['Country','Year'])['Medal'].sum()

When your column dtype is str then the sum function just concatenates all the strings

Upvotes: 4

Related Questions