简体   繁体   中英

.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']) . You can check this in the documentation

尝试

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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