I have a sample df
A | B |
---|---|
X | 30 |
Y | 150 |
Z | 450 |
XX | 300 |
I need to create another column C that buckets column B based on some breakpoints
Breakpts = [50,100,250,350]
A | B | C |
---|---|---|
X | 30 | '0-50' |
Y | 150 | '100-250' |
Z | 450 | '>350' |
XX | 300 | '250-350' |
I have the following code that works
def conditions(i):
if i <=50: return '0-50'
if i > 50 and i <=100: return '50-100'
if i > 100 and i <=250: return '100-250'
if i > 250 and i <=350: return '250-350'
if i > 350: return '>350'
df['C']=df['B'].apply(conditions)
However I would like to make the breakpts dymanic. So if I use a different breakpts like [100,250,300,400] the code should automatically create different buckets based on the breakpts.
Any ideas on how to do this?