我有来自pytho pandas的groupeby对象。我可以在分组对象和size方法上使用np.sum方法。但是,当我尝试使用np.mean和np.std方法时,它给出了一条错误消息。
3093 if len(new_blocks) == 0:
-> 3094 raise DataError('No numeric types to aggregate')
3095
3096 return data.items, new_blocks
DataError: No numeric types to aggregate
我的操作代码如下:
# create a groupby object. the groupby key is 0
final_grouped = final.groupby(0)
# call the sum function on the grouped by object
y = final_grouped.aggregate(np.sum)
# call the size method on the grouped object
y['size']=final_grouped.size()
# call the std and mean function on the grouped object
print(final_grouped.agg(np.mean))
数据如下所示:
index 0 population
0 United Kingdom Europe 63.871
1 Brazil South America 205.915
2 Russian Federation Europe 143.5
3 Germany Europe 80.3697
4 Spain Europe 46.4434