Use of groupby in a function for dataframe

di il
3 risposte

Use of groupby in a function for dataframe

Salve a tutti,
sono bloccata con un task sull'uso di groupby in un dataframe.
Ho un dataframe in un file csv "athletes.csv" che contiene i seguenti dati:

,forename,surname,gender,age,100m,200m,400m,800m,1500m
0,Migdalia,Parrish,F,18,11.08,29.0,59.41,122.05,259.11
1,Valerie,Lee,F,10,17.23,46.0,100.02,232.64,480.95
2,John,Debnam,M,17,10.81,25.89,50.6,110.29,232.39
3,Roy,Miller,M,10,19.18,46.74,95.32,201.14,430.27
4,Aida,Aumiller,F,11,15.3,41.83,81.06,189.03,394.9
5,Marcia,Brown,F,19,11.13,24.62,57.59,119.13,256.37
6,Harry,Knows,M,16,12.39,25.94,49.67,106.56,237.14
7,Barry,Lennon,M,14,11.15,23.56,46.46,110.89,230.49
8,Lilia,Armstrong,F,13,8.84,25.09,59.54,128.95,258.47
9,Johnny,Casey,M,15,9.65,22.67,49.46,112.85,233.87
10,Donald,Taylor,M,15,11.74,22.42,49.22,114.62,224.63
11,Martha,Woods,F,14,9.01,24.34,55.25,118.8,254.87
12,Diane,Lauria,F,15,8.99,27.92,54.79,119.89,249.21
13,Yvonne,Pumphrey,F,16,8.84,27.29,57.63,123.13,247.41
14,Betty,Stephenson,F,14,11.04,28.73,59.05,126.29,256.44
15,Lilia,Armstrong,F,12,11.31,34.43,74.28,150.05,321.07

L'obiettivo è di fare una funzione che chiama un'altra funzione che prende 3 attributi:
- dataframe df
- Età 15 anni
- Il valor medio di tutti gli eventi (100m,200m,400m,800m,1500m)) per gli atleti di 15 anni di età.

La funzione deve raggruppare gli atleti per gender e resettare gli indexes.
Praticamente input ed output sono così:

Input:
age_statistics(df,15,'mean')

Output:
gender 100m 200m 400m 800m 1500m
mean mean mean mean mean
0 F 8.990 27.920 54.790 119.890 249.210
1 M 10.695 22.545 49.340 113.735 229.250

Sono completamente bloccata su come fare il coding della funzione che che raggruppa gli atleti di 15 anni in femmine e maschi e per ogni categoria (100m, 200m,...) calcola il valore medio.


# function to groupby 
def age_statistics(df,age,mean):
# no idea how to build it  
    aggregated_dataframe = aggregated_dataframe.reset_index(drop=False)
    return aggregated_dataframe

# main function
def main(filename='athletes.csv'):
    df = pd.read_csv(filename, index_col=0)
    df['100m'] = df['100m'].astype(float)
    df['200m'] = df['200m'].astype(float)
    df['400m'] = df['400m'].astype(float)
    df['800m'] = df['800m'].astype(float)
    df['1500m'] = df['1500m'].astype(float)
    print(age_statistics(df,15,'mean'))

# Do not edit this
if __name__ == "__main__":
  main()
  
Qualcuno mi può aiutare?

3 Risposte

  • Re: Use of groupby in a function for dataframe

    
    # function to groupby 
    def age_statistics(df,age,mean):
        df = df[df[' age'] == age].groupby([' gender']).agg({'100m' : mean, '200m' : mean, '400m':mean,'800m':mean, '1500m':mean})
        df= df.reset_index(drop=False)
        return df
    
    If you run this function you will get following output.
    This function will given mean of required column and also sort as per age.

    >>> age_statistics(df,age=15,mean = 'mean')
    gender 100m 200m 400m 800m 1500m
    0 F 8.990 27.920 54.79 119.890 249.21
    1 M 10.695 22.545 49.34 113.735 229.25
  • Re: Use of groupby in a function for dataframe

    Wow, grazie 1000, nel frattempo avevo trovato un'altra soluzione mooolto più complicata, la tua è molto più elegante e corta!!
    Grazie ancora!!
  • Re: Use of groupby in a function for dataframe

    No problem, It's my pleasure!!
Devi accedere o registrarti per scrivere nel forum
3 risposte