I have created a small function to assign a string value to a column based on ranges from another column ie: 3.2 == '0-6m', 7 == '6-12m'
But I am getting this error: TypeError: 'float' object is not subscriptable
Dataframe
StartingHeight
4.0
3.2
8.0
32.0
12.0
18.3
Expected output:
StartingHeight height_factor
4.0 0-6m
3.2 0-6m
8.0 6-12m
32.0 >30m
12.0 6-12m
18.3 18-24m
Code:
def height_bands(hbcol):
"""Apply string value based on float value ie: 6.2 == '6-12m
hb_values = ['0-6m', '6-12m', '12-18m', '18-24m', '24-30m', '>30m']"""
if (hbcol['StartingHeight'] >= 0) | (hbcol['StartingHeight'] < 6.1):
return '0-6m'
elif (hbcol['StartingHeight'] >= 6.1) | (hbcol['StartingHeight'] < 12):
return '6-12m'
elif (hbcol['StartingHeight'] >= 12) | (hbcol['StartingHeight'] < 18):
return '12-18m'
elif (hbcol['StartingHeight'] >= 18) | (hbcol['StartingHeight'] < 24):
return '18-25m'
else:
return '>30m'
df1['height_factor'] = df1.apply(lambda x: height_bands(x['StartingHeight']), axis=1)
Thanks for your help!