简体   繁体   English

.groupby .agg 在 python 中不能按预期工作

[英].groupby .agg does not work as expected in python

My dataframe is like:我的数据框是这样的:

在此处输入图片说明

I used the following code to calculate the sum, mean and std of a column for each group, but it gave me an error.我使用以下代码计算每个组的列的总和、均值和标准差,但它给了我一个错误。

New11.groupby(level=0)['Population'].agg([np.sum,np.mean,np.std])
DataError: No numeric types to aggregate

However, the following code worked fine.但是,以下代码运行良好。

New11.groupby(level=0)['Population'].agg([np.sum])

This code gives an error.此代码给出了错误。

New11.groupby(level=0)['Population'].agg([np.average])
AttributeError: 'float' object has no attribute 'dtype'

How should I modify my code to make it work?我应该如何修改我的代码以使其工作?

Please try convert the values of 'Population' in float32 of the next form New11['Population]=np.float32(New11['Population']) .请尝试在下一个形式New11['Population]=np.float32(New11['Population']) float32 中转换 'Population' 的值。 You can check this in the documentation您可以在文档中查看

尝试

New11.groupby(level=0).agg({'Population': ['sum', 'mean', 'std']})

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

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