7

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.

Goh-shans
  • 125
  • 1
  • 12
Tapal Goosal
  • 361
  • 4
  • 13

1 Answers1

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"])
Augustin
  • 2,444
  • 23
  • 24
BENY
  • 317,841
  • 20
  • 164
  • 234