Home > Net >  How to encode the data based on range of numbers
How to encode the data based on range of numbers

Time:03-12

import pandas as pd
from sklearn import preprocessing

my_data = {
    "Marks" : [50, 62, 42, 90, 12],
    "Exam" : ['FirstSem', 'SecondSem', 'ThirdSem', 'FourthSem','FifthSem']
        }
blk = pd.DataFrame( my_data )  
print( blk )

Required solution
   Marks       Exam
0      1   FirstSem
1      1  SecondSem
2      0   ThirdSem
3      1  FourthSem
4      0   FifthSem

Is there any solution to encode the values if marks greater than 45 is 1 and marks less than 45 is 0

CodePudding user response:

blk["Marks"] = np.where(blk["Marks"]>45,1,0)
blk
   Marks       Exam
0      1   FirstSem
1      1  SecondSem
2      0   ThirdSem
3      1  FourthSem
4      0   FifthSem
  • Related