Search code examples
pythonpandasmedian

Assign median values by separating data to bins


I have a dataframe which I want to separate into bins and assign each bin the median value of the values in that bin.

   POA   Egrid           
   200   1.17
   205   0.63
   275   1.08
   325   1.22
   350   0.57

The result should look like

   POA       Egrid           
 (200,300)   Median of (1.17,0.63,1.08)
 (300,400)   Median of (1.22,0.57)

I tried to write two loops, but couldn't figure out the median part. Any help would be good.


Solution

  • Do with

    s=df.groupby(pd.cut(df.POA,[100,200,300])).Egrid.median().reset_index()
              POA  Egrid
    0  (100, 200]  1.170
    1  (200, 300]  0.855