0

I have a dataframe with id, value, missing values(this is a %age). I then want to have another column that has range that if the missing value is

  • <=25 then it should return 1
  • <=50 return 2
  • <=75 return 3
  • <=80 return 4

What best way can i do this

example of dataframe

df = pd.DataFrame({
'id': ['1245', '1323', '1784', '1557','1456'],
'value': [11558522, 12323552, 13770958, 18412280, 13770958],
'missing value': [34, 56, 80, 5 76]
}) 
Tamarie
  • 125
  • 2
  • 6
  • 18

2 Answers2

0

You could use pandas cut to divide ur column into bins

df['range'] = pd.cut(df['missing value'],bins=[0,25,50,75,80], labels=[1,2,3,4])

      id    value   missing value   range
0   1245    11558522    34          2
1   1323    12323552    56          3
2   1784    13770958    80          4
3   1557    18412280    5           1
4   1456    13770958    76          4
sammywemmy
  • 27,093
  • 4
  • 17
  • 31
0

Another way to do so is to use the apply method as:

def RangeDefiner(val):
    if val<=25:
        return 1
    elif val<=50:
        return 2
    elif val<=75:
        return 3
    elif val<=80:
        return 4

And to get the ranges:

df['ranges']=df.apply(lambda x: RangeDefiner(x['missing value']), axis=1)

And the output for your input:

     id      value    missing value ranges
0   1245    11558522    34            2
1   1323    12323552    56            3
2   1784    13770958    80            4
3   1557    18412280    5             1
4   1456    13770958    76            4
Tayyab
  • 1,207
  • 8
  • 29