我有一个数据框如下
id1, id2, thumb_up
1, 1, 1
1, 2, -1
1, 3, 1
2, 1, 1
2, 3, -1
and so on
我想创建以下数据框
id1, thumb_up_prob
1, 0.66 ( 2 positive feedback and 1 negative = 2/3)
2, 0.5 (1 positive and 1 negative feedback = 1/2)
等等.. 所以基本上我想 groupby id1 并计算概率。 我如何在 Pandas 中做到这一点。
最佳答案
- 检查
df.thumb_up
是否等于 1 groupby
df.id1
- 因为
bool
是int
的子集,我们调用mean
df.thumb_up.eq(1).groupby(df.id1).mean()
关于python - 如何分组并计算 Pandas 的概率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43014944/