pandas 产品,而不是总数或数量 [英] pandas group by product instead of sum or count
本文介绍了 pandas 产品,而不是总数或数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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屋!
查看全文