Discretizing a Pandas column based on custom ranges
Asked Answered
J

1

8

Is there a way to discretize a column from a Pandas dataframe based on custom limits (meaning the ranges are not of equal length)? Previous questions asked here don't cover this case.

For example, assume we want to convert numeric grades (out of 4) into bins as follows:

3.75 to 4: Excellent

3.5 to 3.75: Very good

3.25 to 3.5: Good

3 to 3.25: Average

2.5 to 3: Bad

Below 2.5: Very bad

I know it can be done using a series of ifs and elses, but I was looking for a cleaner and more flexible (for higher number of bins) way to do that.

Jacquelynejacquelynn answered 6/7, 2018 at 21:59 Comment(0)
R
10

You can using cut

pd.cut(df["Yourcolumns"],
       bins=[0, 2.5, 3, 3.25, 3.5, 3.75, 4], 
       labels=["Very bad", "Bad", "Average", "good", "Very good", "Excellent"])
Radiology answered 6/7, 2018 at 22:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.