简体   繁体   English

如何使熊猫 groupby().count() 求和而不是行?

[英]How to make pandas groupby().count() sum values rather than rows?

I am aware that size() is the one that count rows, and count() should be counting values.我知道 size() 是计算行数的,而 count()应该是计数值。 However, that isn't happening.然而,这并没有发生。 When I compare the .count() vs .size(), I get the same result, when they should be greatly different.当我比较 .count() 和 .size() 时,我得到了相同的结果,但它们应该有很大的不同。 My code:我的代码:

import pandas as pd

df = pd.read_csv("TEST1.csv",sep=';')

df.columns = ['Time','Type','Value','Size','Buyer']

df['Value'] = pd.to_numeric(df['Value'])
df['Size'] = pd.to_numeric(df['Size'])

n_of_buyers = df.groupby("Buyer")["Size"].count()
print(n_of_buyers)

I thought that by having the df['Size'] = pd.to_numeric(df['Size']) I shouldn't have this issue, and yet...我认为通过df['Size'] = pd.to_numeric(df['Size'])我不应该有这个问题,但是......

Thanks ever so much.非常感谢。

我认为您要使用的是GroupBy.sum

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM