简体   繁体   中英

Creating new column with ranges based on existing column

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

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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM