0

I have one column with different values. I would like to make a new column that will group those values into ranges (eg. 0-5, 5-10, 10-20, etc.) How should I do it using pandas library?

df['price_group']

0         475000
1         720000
2         232000
3         728000
4         706000
          ...   
21615     485000
21616    1008000
21617     283000
21618     293550
21619     250000

catfood
  • 1
  • 3

1 Answers1

0

Something wrong with your question: what happens with boundary class values? Does 5 belong to the 0-5 class or to the 5-10 class?

Anyways, your best bet would probably be something like this. I'm assuming your classes are the following ones: ]-inf , -1], [0 , 4], [5 , 9], [10 , 19] and [20 , inf[

import pandas as pd

# Declare your function
def my_custom_function(length):
    """ you could also return int, float or other objects"""

    if length < 0:
        return "negative"
    elif length < 5:
        return "0m - 4m"
    elif length < 10:
        return "5m - 9m"
    elif length < 20:
        return "10m - 19m"
    else:
        return "20m and +"

# Add your desired column
df['sampled_length'] = df['length'].apply(lambda x: my_custom_function(x))