2

I have following dataframe

name,value
a,100
b,200
c,150
d,300
e,400
f,200
g,100

i have list of ranges like [0-100,100-200,more than 200]

based on this i have to take count of records in above dataframe.

i need output like

category,count
0-100,2
100-200,3
mor than 200,3
Sai Rajesh
  • 1,882
  • 5
  • 22
  • 41

1 Answers1

8

Use groupby + cut:

bins = [-1, 100, 200, np.inf]
labels=['0-100','100-200','more than 200']
df=df.groupby(pd.cut(df['value'], bins=bins, labels=labels)).size().reset_index(name='count')
print (df)
           value  count
0          0-100      2
1        100-200      3
2  more than 200      2
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252