Python groupby sum显示错误的outpu

2024-06-02 05:58:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用的是groupby sum,但得到的输出错误:

This is my dataframe

虽然medal列只包含01的值,但是我在执行以下代码之后得到了这个输出。你知道吗

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


Tags: 代码testnew错误yearcountrysumgroupby
1条回答
网友
1楼 · 发布于 2024-06-02 05:58:32

您的Medal列是str,首先转换为int,然后求和:

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

当列dtypestr时,sum函数只是将所有字符串串联起来

相关问题 更多 >