Skip to content Skip to sidebar Skip to footer

How To Group By List Ranges Of Value In Python Pandas

I have following dataframe 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

Solution 1:

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

Post a Comment for "How To Group By List Ranges Of Value In Python Pandas"