Home > Net >  Pandas GroupBy based on range and find min and max
Pandas GroupBy based on range and find min and max

Time:10-15

I want to find max and minimum value of each group based on the range. Here is the dataframe

df=pd.DataFrame({
   'A':['a','b','a','b','a','b','a','b'],
   'B':[0.1,0.2,0.3,0.4,0.5,0.46,0.76,.9],
   'C':[10,20,40,54,67,35,90,33]
    })

I want to calculate minimum value but based on range of 0.2-0.5 that if value lies in that range.

I have tried this

df.groupby(pd.cut(df["B"], np.arange(0.2, 0.5))).min()

But it throws

AssertionError: `result` has not been initialized.

The expected answer is

pd.DataFrame(
    {'A':['a','b'],
    'Min':[40,20]
     })

CodePudding user response:

You don't want to group on a range like in the linked post but rather to filter before grouping.

out = (df[df['B'].between(0.2, 0.5)]
       .groupby('A', as_index=False)
       ['C'].min()
       )

Output:

   A   C
0  a  40
1  b  20

CodePudding user response:

Let's query the dataframe first then do groupby with named aggregation

out = (df.query('0.2 <= B <= 0.5')
       .groupby('A', as_index=False)
       .agg(Min=('C', min)))
print(out)

   A  Min
0  a   40
1  b   20
  • Related