我正在使用 groupby sum,但得到了错误的输出:
虽然奖牌列只包含 0
或 1
的值,但我在执行以下代码后得到了这个输出。
test=oly_new.groupby(['Country','Year'])['Medal'].sum()
最佳答案
你的Medal
列是一个str
,先转换成int
然后求和:
oly_new['Medal'] = oly_new['Medal'].astype(int)
test=oly_new.groupby(['Country','Year'])['Medal'].sum()
当您的列 dtype
是 str
时,sum
函数只是连接所有字符串
关于Python pandas groupby 总和显示错误的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55490821/