Use of groupby in a function for dataframe


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

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


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.

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)

# Do not edit this
if __name__ == "__main__":

Anybody can help with that?

I’m not familiar with this library, but I’m pretty sure you’d have to indicate that you want these calculation to happen with the “gender” column. On your line where you do reset_index I’m thinking you’re missing something that indicates this.

The other option is to just manually filter out the M and F rows into two data frames, do the stats on those, then output the results, but I suspect that’d be “cheating” as there’s most likely a way to say “do this summary group by gender”.

What library is this?

1 Like

Hi zedshaw, thanks for your reply.
The library is pandas.
The reset_index is not important, it’s the only thing I understood from the assignment that adds on the left column of the output dataframe the indexes 0,1,…
My problem is how to structure the function age_statistics, I really don’t know from where to start.

I don’t know pandas at all, but it sounds like you’re attempting to look at this empty function and write the code straight without any help, and that’s nearly impossible if you don’t know the library deeply. What happens is you are told “make this do summary stats for F/M gender”. That’s it. Just a goal. You’re not given the code and you have to figure out how to go from nothing to code.

The problem is, not even top professionals like me can go from nothing working code using pure code if they’ve never used the library. We all write out notes and comments and then “fill in the blanks” to figure it out.

What you should do is start with an human description of the steps, convert those to “pseudo-code”, then slowly convert that to real working code, running it constantly as you go.

Step 1:

def age_statistics(df, age, mean):
   # get the stuff from the thing
   # extract the other stuff
   # if the stuff is X then format it

Keep in mind this is fake as it’s just a demo. Once you have the idea written out in steps in your own words, you convert it to fake code comments:

def age_statistics(df, age, mean):
  # thing = stuff get
  # other_stuff = thing get
  # if stuff == X
  #    # format X

Then you take each of those and research in pandas how to do that line. After you fill in each line with code you think works run it. run it constantly. If you aren’t running this at least 1-2 times per line of code you write, then delete it and do it again.

What I mean by “fill in the blanks” is like this:

def age_statistics(df, age, mean):
   # thing = stuff get
   thing = stuff.get("F")
   # other_stuff = thing get
   # if stuff == X
   # format X

RUN IT! Then next line (these are all fake):

def age_statistics(df, age, mean):
   # thing = stuff get
   thing = stuff.get("F")

   # other_stuff = thing get
   other_stuff = thing.get("other stuff")

   # if stuff == X
   # format X


def age_statistics(df, age, mean):
   # thing = stuff get
   thing = stuff.get("F")

   # other_stuff = thing get
   other_stuff = thing.get("other stuff")

   # if stuff == X
   if other_stuff == "X":
      # format X

Notice on the last one I did 2 lines since that’s how you make the if work. Now, RUN IT.

This is the process I use when I’m working with a library I don’t know, but I do know what I need to get done. Try it.

1 Like

Hi zedshaw,many thanks, I’ll try to work out the code following your steps!!