问题描述
我最近刚刚从 R 切换到 python,并且在再次习惯数据帧而不是使用 R 的 data.table 时遇到了一些麻烦.我遇到的问题是我想获取一个字符串列表,检查一个值,然后将该字符串的计数相加 - 由用户分解.所以我想把这些数据:
I just recently made the switch from R to python and have been having some trouble getting used to data frames again as opposed to using R's data.table. The problem I've been having is that I'd like to take a list of strings, check for a value, then sum the count of that string- broken down by user. So I would like to take this data:
然后返回:
在我用 R 代码做之前(使用 data.table)
Before I did it with the R code (using data.table)
但是,我最近使用 Python 的所有尝试都失败了:
However all of my recent attempts with Python have failed me:
提前感谢您!这似乎是一个简单的问题,但我在任何地方都找不到.
Thank you in advance! it seems like a simple question however I couldn't find it anywhere.
推荐答案
为了补充 unutbu 的答案,这里有一个在 groupby 对象上使用 apply
的方法.
To complement unutbu's answer, here's an approach using apply
on the groupby object.
这篇关于pandas 聚合的条件总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!