MBasith
MBasith

Reputation: 1499

Python Pandas Groupby and Aggregation

Hi I am trying to aggregate some data in a dataframe by using agg but my initial statement mentioned a warning "FutureWarning: using a dict on a Series for aggregation is deprecated and will be removed in a future version". I rewrote it based on Pandas documentation but instead of getting the right column label I am getting a function label. example: "". How can I correct the output so that the labels match the deprecated output above with column names std, mean, size, sum?

Deprecated Syntax Command:

Top15.set_index('Continent').groupby(level=0)['Pop Est']
      .agg({'size': np.size, 'sum': np.sum, 'mean': np.mean, 'std': np.std})

Deprecated Syntax Output:

                        std          mean  size           sum
Continent                                                    
Asia           6.790979e+08  5.797333e+08   5.0  2.898666e+09
Australia               NaN  2.331602e+07   1.0  2.331602e+07
Europe         3.464767e+07  7.632161e+07   6.0  4.579297e+08
North America  1.996696e+08  1.764276e+08   2.0  3.528552e+08
South America           NaN  2.059153e+08   1.0  2.059153e+08

New Syntax Command:

Top15.set_index('Continent').groupby(level=0)['Pop Est']\
    .agg(['size', 'sum', 'mean', 'std'])\
    .rename(columns={'size': np.size, 'sum': np.sum, 'mean': np.mean, 'std': np.std})

New Syntax Output:

               <function size at 0x0000000002DE9950>  <function sum at 0x0000000002DE90D0>  <function mean at 0x0000000002DE9AE8>  <function std at 0x0000000002DE9B70>
Continent                                                                                                                                                              
Asia                                               5                          2.898666e+09                           5.797333e+08                          6.790979e+08
Australia                                          1                          2.331602e+07                           2.331602e+07                                   NaN
Europe                                             6                          4.579297e+08                           7.632161e+07                          3.464767e+07
North America                                      2                          3.528552e+08                           1.764276e+08                          1.996696e+08
South America                                      1                          2.059153e+08                           2.059153e+08                                   NaN

Dataframe:

                    Rank  Documents  Citable documents  Citations  Self-citations  Citations per document  H index  Energy Supply  Energy Supply per Capita  % Renewable          2006          2007          2008          2009          2010          2011          2012          2013          2014          2015       Pop Est      Continent
Country                                                                                                                                                                                                                                                                                                                                          
China                  1     127050             126767     597237          411683                    4.70      138   1.271910e+11                      93.0    19.754910  3.992331e+12  4.559041e+12  4.997775e+12  5.459247e+12  6.039659e+12  6.612490e+12  7.124978e+12  7.672448e+12  8.230121e+12  8.797999e+12  1.367645e+09           Asia
United States          2      96661              94747     792274          265436                    8.20      230   9.083800e+10                     286.0    11.570980  1.479230e+13  1.505540e+13  1.501149e+13  1.459484e+13  1.496437e+13  1.520402e+13  1.554216e+13  1.577367e+13  1.615662e+13  1.654857e+13  3.176154e+08  North America
Japan                  3      30504              30287     223024           61554                    7.31      134   1.898400e+10                     149.0    10.232820  5.496542e+12  5.617036e+12  5.558527e+12  5.251308e+12  5.498718e+12  5.473738e+12  5.569102e+12  5.644659e+12  5.642884e+12  5.669563e+12  1.274094e+08           Asia
United Kingdom         4      20944              20357     206091           37874                    9.84      139   7.920000e+09                     124.0    10.600470  2.419631e+12  2.482203e+12  2.470614e+12  2.367048e+12  2.403504e+12  2.450911e+12  2.479809e+12  2.533370e+12  2.605643e+12  2.666333e+12  6.387097e+07         Europe
Russian Federation     5      18534              18301      34266           12422                    1.85       57   3.070900e+10                     214.0    17.288680  1.385793e+12  1.504071e+12  1.583004e+12  1.459199e+12  1.524917e+12  1.589943e+12  1.645876e+12  1.666934e+12  1.678709e+12  1.616149e+12  1.435000e+08         Europe
Canada                 6      17899              17620     215003           40930                   12.01      149   1.043100e+10                     296.0    61.945430  1.564469e+12  1.596740e+12  1.612713e+12  1.565145e+12  1.613406e+12  1.664087e+12  1.693133e+12  1.730688e+12  1.773486e+12  1.792609e+12  3.523986e+07  North America
Germany                7      17027              16831     140566           27426                    8.26      126   1.326100e+10                     165.0    17.901530  3.332891e+12  3.441561e+12  3.478809e+12  3.283340e+12  3.417298e+12  3.542371e+12  3.556724e+12  3.567317e+12  3.624386e+12  3.685556e+12  8.036970e+07         Europe
India                  8      15005              14841     128763           37209                    8.58      115   3.319500e+10                      26.0    14.969080  1.265894e+12  1.374865e+12  1.428361e+12  1.549483e+12  1.708459e+12  1.821872e+12  1.924235e+12  2.051982e+12  2.200617e+12  2.367206e+12  1.276731e+09           Asia
France                 9      13153              12973     130632           28601                    9.93      114   1.059700e+10                     166.0    17.020280  2.607840e+12  2.669424e+12  2.674637e+12  2.595967e+12  2.646995e+12  2.702032e+12  2.706968e+12  2.722567e+12  2.729632e+12  2.761185e+12  6.383735e+07         Europe
South Korea           10      11983              11923     114675           22595                    9.57      104   1.100700e+10                     221.0     2.279353  9.410199e+11  9.924316e+11  1.020510e+12  1.027730e+12  1.094499e+12  1.134796e+12  1.160809e+12  1.194429e+12  1.234340e+12  1.266580e+12  4.980543e+07           Asia
Italy                 11      10964              10794     111850           26661                   10.20      106   6.530000e+09                     109.0    33.667230  2.202170e+12  2.234627e+12  2.211154e+12  2.089938e+12  2.125185e+12  2.137439e+12  2.077184e+12  2.040871e+12  2.033868e+12  2.049316e+12  5.990826e+07         Europe
Spain                 12       9428               9330     123336           23964                   13.08      115   4.923000e+09                     106.0    37.968590  1.414823e+12  1.468146e+12  1.484530e+12  1.431475e+12  1.431673e+12  1.417355e+12  1.380216e+12  1.357139e+12  1.375605e+12  1.419821e+12  4.644340e+07         Europe
Iran                  13       8896               8819      57470           19125                    6.46       72   9.172000e+09                     119.0     5.707721  3.895523e+11  4.250646e+11  4.289909e+11  4.389208e+11  4.677902e+11  4.853309e+11  4.532569e+11  4.445926e+11  4.639027e+11           NaN  7.707563e+07           Asia
Australia             14       8831               8725      90765           15606                   10.28      107   5.386000e+09                     231.0    11.810810  1.021939e+12  1.060340e+12  1.099644e+12  1.119654e+12  1.142251e+12  1.169431e+12  1.211913e+12  1.241484e+12  1.272520e+12  1.301251e+12  2.331602e+07      Australia
Brazil                15       8668               8596      60702           14396                    7.00       86   1.214900e+10                      59.0    69.648030  1.845080e+12  1.957118e+12  2.056809e+12  2.054215e+12  2.208872e+12  2.295245e+12  2.339209e+12  2.409740e+12  2.412231e+12  2.319423e+12  2.059153e+08  South America

Upvotes: 0

Views: 1647

Answers (1)

howard roark
howard roark

Reputation: 638

Try using just this:

Top15.set_index('Continent').groupby(level=0)['Pop Est'].agg(['size', 'sum', 'mean', 'std'])

Upvotes: 1

Related Questions