Reputation: 423
I have a DataFrame:
ID Value
1 1
2 2
3 1
4 1
5 2
6 3
7 4
8 5
9 10
10 15
I want to groupby by my value and count the ID, in the customized range: <=2, 3-9, >= 10
The results would look like:
Value ID
<=2 5
3-9 3
>= 10 2
Upvotes: 1
Views: 89
Reputation: 6159
I don't think you need groupby.
labels = ['<=2', '3-9', '>=10']
bins = [0,2,9, np.inf]
pd.cut(df['Value'],bins=bins,labels=labels).value_counts().reset_index()
#out[]
index Value
<=2 5
3-9 3
>=10 2
Upvotes: 2