The pandas GroupBy operator

In pandas, we can achieve the same result by using the GroupBy function:

    In [6]: import pandas as pd
    importnumpy as np
    In [7]: goal_stats_df=pd.read_csv('champ_league_stats_semifinalists.csv')
    
    In [27]: goal_stats_df['GoalsPerGame']=     goal_stats_df['Goals']/goal_stats_df['GamesPlayed']
    
    In [27]: goal_stats_df['GoalsPerGame']= goal_stats_df['Goals']/goal_stats_df['GamesPlayed']
    
    In [28]: goal_stats_df
    Out[28]: Club           Player      Goals GamesPlayedGoalsPerGame
    0       Atletico Madrid Diego Costa   8       9        0.888889
    1       Atletico Madrid ArdaTuran    4       9         0.444444
    2       Atletico Madrid RaúlGarcía   4       12        0.333333
    3       Atletico Madrid AdriánLópez  2       9         0.222222
    4       Atletico Madrid Diego Godín   2       10        0.200000
    5       Real Madrid  Cristiano Ronaldo 17      11        1.545455
    6       Real Madrid     Gareth Bale   6       12        0.500000
    7       Real Madrid     Karim Benzema 5       11        0.454545
    8       Real Madrid     Isco          3       12        0.250000
    9       Real Madrid     Ángel Di María 3      11        0.272727
    10      Bayern Munich   Thomas Müller  5       12        0.416667
    11      Bayern Munich   ArjenRobben   4       10        0.400000
    12      Bayern Munich   Mario Götze    3       11        0.272727
    13      Bayern Munich  BastianSchweinsteiger 3   8     0.375000
    14      Bayern Munich  MarioMandzukić  3       10        0.300000
    15      Chelsea        Fernando Torres  4       9         0.444444
    16      Chelsea        Demba Ba         3       6         0.500000
    17      Chelsea        Samuel Eto'o     3       9         0.333333
    18      Chelsea        Eden Hazard      2       9         0.222222
    19      Chelsea        Ramires          2       10        0.200000
    
    In [30]: grouped = goal_stats_df.groupby('Club')
    
    In [17]: grouped['GoalsPerGame'].aggregate(np.max)
    Out[17]: Club
             Atletico Madrid    0.888889
             Bayern Munich      0.416667
             Chelsea            0.500000
             Real Madrid        1.545455
             Name: GoalsPerGame, dtype: float64
    
    In [22]: grouped['GoalsPerGame'].apply(np.max)
    
    Out[22]: Club
             Atletico Madrid    0.888889
             Bayern Munich      0.416667
             Chelsea            0.500000
             Real Madrid        1.545455
             Name: GoalsPerGame, dtype: float64
  
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.149.28.145