I am working with the following dataframe which I created from a much larger csv file with additional information in columns not needed:
df_avg_tot_purch = df_purchase_data.groupby([“SN”, “Gender”])[“Price”].agg(lambda x: x.unique().mean()) df_avg_tot_purch.head()
This code results in the following:
SN Gender Adairialis76 Male 2.28 Adastirin33 Female 4.48 Aeda94 Male 4.91 Aela59 Male 4.32 Aelaria33 Male 1.79 Name: Price, dtype: float64
I now need to have this dataframe only show the male gender. The point of the project here is to find all the individuals (which may repeat in the rows), determine the average of each of their purchases. I did it this way because I also need to run another for females and “others” in the column.
after groupby the keys on which you grouped become indices, so now you have to either reset index to change them into normal columns, or explicitly use index while subsetting
df_avg_tot_purch = df_avg_tot_purch.reset_index() df_avg_tot_purch[df_avg_tot_purch['Gender'] == 'Male']