Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Group data by ranges in pandas

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading