Home > Blockchain >  Group By With Custom Function in Pandas
Group By With Custom Function in Pandas

Time:08-19

Help I got stuck with this problem :( How to solve this question?

  1. Groupby the data based on the criteria cut (quality), then do aggregation with the standard deviation function on the column carat and the value range function between max and min on the column price. enter image description here enter link description here

CodePudding user response:

If i understand the question, you want to groupby cut and aggregate the carat column by std, and the price column both by min and max. In that case you can try:

df.groupby('cut')[['carat', 'price']].agg({'carat':'std','price':['max','min']}) 

Output:

         carat      price
         std        max min
cut         
Fair    NaN         337 337
Good    0.029665    351 327
Ideal   0.043493    348 326
Premium 0.048028    355 326
Very Good   0.033310    402 336

Or If you want to see the range only you can do:

df.groupby('cut')[['carat', 'price']].agg({'carat':'std','price':lambda x: x.max()-x.min()})

Output:
              carat  price
cut                       
Fair            NaN      0
Good       0.029665     24
Ideal      0.043493     22
Premium    0.048028     29
Very Good  0.033310     66
  • Related