简体   繁体   中英

Python pandas groupby sum displaying wrong output

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

这是我的数据框

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()

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

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