Skip to content
Advertisement

How to apply pandas groupby to a dataframe to use both rows and columns when calculating a mean

I have a dataframe df in the format:

JavaScript

And I am looking to group it such that I intersect the Rating as the index, the Height (split into buckets) as the columns, and within the individual cells have the average value for the combination of Grade and Height.

So, the output dataframe would look something like this:

JavaScript

where the x’s are the calculated mean speed*value.

I have attempted unsuccessfully with something like:

JavaScript

but I can’t quite figure out a method that might work not throwing errors or an empty df.

Would you have any ideas I can try out?

Advertisement

Answer

  • Use pd.cut to break your Height Column into bins.
  • Create a new column of Speed * Value
  • Pivot your table, mean is the default pivot function.
    • dropna=False is used so that even null bins are shown.
JavaScript

Output:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement