Search code examples
pythonmingroup

Find minimum of two different groupedby columns in Python


I am just starting to learn python and have not been able to find the answer to this anywhere.

Here's what I have so far:

(df.groupby(['State', 'County', 'Family']).AP.agg(Minimum = ("min"),
                                                  Maximum = ("max"),
                                                  Sum = ("sum"),
                                                  Volume = ("count"),
                                                  Average = ("mean"),
                                                  Mode = (lambda x: x.value_counts().index[0])
                                                  ).reset_index().round(0))

Now I need to add a column that has the minimum of the average and mode columns, is this possible?

So for example:

State County Family Minimum Maximum Sum Volume Average Mode Min of Avg & Mode
FL ALACHUA Interior 400 500 5025 12 419.0 400 400
FL WALTON Interior 500 1400 10432 14 515.0 550 515

Thanks!


Solution

  • You can use np.where() to select the smaller value of 2 columns.

    import numpy as np
    
    df['Min of Avg & Mode'] = np.where(df['Average'] > df['Mode'], df['Mode'], df['Average'])