Python Forum
Use of groupby in a function with Pandas
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Use of groupby in a function with Pandas
#1
Hello,

I'm totally stuck with a task on using groupby in a dataframe.

I have the following df in a csv file 'athletes.csv:

,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

The task is to call (and print) from a main function another function which takes three attributes:
- The dataframe df
- The age 15
- The mean value for all events (100m,200m,400m,800m,1500m)) for the age 15
The function should be grouped by gender and should reset the index.

The output should be like the below.
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
I'm completely stuck with the function that groups the 15 years'old athletes in females and males and for each gender calculates the mean value for each event.

# 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()
Anybody can help with that?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to groupby Months showing average order value - Pandas & matplotlib Rwood90 0 1,858 Oct-20-2020, 12:53 PM
Last Post: Rwood90
  Pandas's regular expression function result is so strange cools0607 6 3,176 Jun-15-2020, 07:34 AM
Last Post: cools0607
  Question about the groupby function new_to_python 7 3,228 Feb-09-2020, 07:52 AM
Last Post: perfringo
  Converting Smiles to Fingerprint with pandas iterrows/apply-function: Jompie96 0 2,494 Jul-04-2019, 01:50 PM
Last Post: Jompie96
  Apply a function with pandas Jompie96 2 2,227 Jun-13-2019, 07:04 PM
Last Post: Jompie96
  groupby and window function lravikumarvsp 3 3,126 May-13-2018, 06:24 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020