Home > Blockchain >  Group data by ranges in pandas
Group data by ranges in pandas

Time:04-24

I have a df as shown:

Value
1
2
3
4
5
4
5
5
6
6
7
7
8
8
9
9

Now I want to divide this df into 5 categories namely as per score range

0-2: Very Low
2-4: Low
4-6: Medium
6-8: High
8-10:Very High

Hence the resultant df should be given as:

Value   Band
1       Very Low
2       Low 
3       Low
4       Med
5       Med
4       Med
5       Med
5       Med
6       High
6       High
7       High
7       High
8       VeryHigh
8       VeryHigh
9       VeryHigh
9       Very High

I know that I can use groupby in pandas to group values in a column but how do I groupby and divide it in 5 categories as shown above

CodePudding user response:

You can use pd.cut, for example:

labels = ["Very Low", "Low", "Medium", "High", "Very High"]

df["Band"] = pd.cut(df["Value"], len(labels), labels=labels)
print(df)

Prints:

    Value       Band
0       1   Very Low
1       2   Very Low
2       3        Low
3       4        Low
4       5     Medium
5       4        Low
6       5     Medium
7       5     Medium
8       6       High
9       6       High
10      7       High
11      7       High
12      8  Very High
13      8  Very High
14      9  Very High
15      9  Very High

Note: If the labels aren't right, you can define your own bins (in the form of list for example)

CodePudding user response:

import pandas as pd

df = pd.DataFrame(list(range(10)),columns=['value'])
df['Band'] = pd.cut(df['value'],bins=[-1,2,4,6,8,10],labels= ['Very Low','low','Medium','High','Very High'])

result :

    value   Band
0   0   Very Low
1   1   Very Low
2   2   Very Low
3   3   low
4   4   low
5   5   Medium
6   6   Medium
7   7   High
8   8   High
9   9   Very High
  • Related