0

I have the following dataframe

PopEst      Continent
Country                                        
China               1.367645e+09           Asia
United States       3.176154e+08  North America
Japan               1.274094e+08           Asia
United Kingdom      6.387097e+07         Europe
Russian Federation  1.435000e+08         Europe
Canada              3.523986e+07  North America
Germany             8.036970e+07         Europe
India               1.276731e+09           Asia
France              6.383735e+07         Europe
South Korea         4.980543e+07           Asia
Italy               5.990826e+07         Europe
Spain               4.644340e+07         Europe
Iran                7.707563e+07           Asia
Australia           2.331602e+07      Australia
Brazil              2.059153e+08  South America

And based on the continent column, I need to add 4 new columns: Count (Nr of countries in the continent) Then Mean, Std, and Sum for the column PopEst

I tried the following:

df.groupby('Continent').aggregate({'size': 'count',
                        'sum': 'sum',
                        'mean': 'mean',
                        'std': 'std'})

but then I get this error:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
/opt/conda/lib/python3.7/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   2889             try:
-> 2890                 return self._engine.get_loc(key)
   2891             except KeyError:

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'size'

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-40-ef7220b45d9d> in <module>
     23                         'sum': 'sum',
     24                         'mean': 'mean',
---> 25                         'std': 'std'})
     26 
     27 print(df)
Luis Valencia
  • 32,619
  • 93
  • 286
  • 506

0 Answers0