首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Pandas:获取组中出现次数最多的字符串值

Pandas:获取组中出现次数最多的字符串值
EN

Stack Overflow用户
提问于 2018-07-11 22:55:21
回答 3查看 2.1K关注 0票数 4

我有以下DataFrame:

item    response
1       A       
1       A       
1       B       
2       A       
2       A   

我想添加一个列,其中包含针对某项的最多给定响应。这应该会导致:

item    response  mostGivenResponse
1       A          A
1       A          A      
1       B          A       
2       C          C
2       C          C

我尝试了这样的东西:

df["responseCount"] = df.groupby(["ItemCode", "Response"])["Response"].transform("count")

df["mostGivenResponse"] = df.groupby(['ItemCode'])['responseCount'].transform(max)

但是mostGivenResponse现在是响应的计数,而不是响应本身。

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51288635

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档