pandas 产品,而不是总数或数量 [英] pandas group by product instead of sum or count

查看:91
本文介绍了 pandas 产品,而不是总数或数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Python熊猫中,我想按列分组数据,然后为每个ID获取行的乘积。总和和计数函数存在,但是一个产品?

  df2 = pd.DataFrame({'X':['B','B','A','A '],'Y':[1,2,3,4]})

print(df2.groupby(['X'])。sum())
Y
x
A 7
B 3

print(df2.groupby(['X'])。count())
Y
X
A 2
B 2

如何取得商品的商品而不是总数或计数?

  print(df2.groupby(['X'])。product())
Y
X
A 12
B 2


解决方案

prod

  df.groupby('X')。prod()


In python pandas, I want to group a dataframe by column and then take the product of the rows for each ID. Sum and count functions exist, but a product?

df2 = pd.DataFrame({'X' : ['B', 'B', 'A', 'A'], 'Y' : [1, 2, 3, 4]})

print(df2.groupby(['X']).sum())
   Y
X   
A  7
B  3

print(df2.groupby(['X']).count())
   Y
X   
A  2
B  2

How can I take the product of the items instead of the sum or count?

print(df2.groupby(['X']).product())
   Y
X   
A  12
B  2

解决方案

There is prod:

df.groupby('X').prod()

这篇关于 pandas 产品,而不是总数或数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆