This is my data set
fake_abalone2
Sex Length Diameter Height Whole Shucked Viscera Shell Rings
Weight Weight Weight Weight
0 M 0.455 0.365 0.095 0.5140 0.2245 0.1010 0.1500 15
1 M 0.350 0.265 0.090 0.2255 0.0995 0.0485 0.0700 7
2 F 0.530 0.420 0.135 0.6770 0.2565 0.1415 0.2100 9
3 M 0.440 0.365 0.125 0.5160 0.2155 0.1140 0.1550 10
4 K 0.330 0.255 0.080 0.2050 0.0895 0.0395 0.0550 7
5 K 0.425 0.300 0.095 0.3515 0.1410 0.0775 0.1200 8
Getting syntax error while using the following method. Please help me out. I want the value in "sex" table to change depending on "Rings" table.If "Rings" value is less than 10 the corresponding "sex" value should be changed to 'K'.Otherwise, no change should be made in "Sex" table.
fake_abalone2["sex"]=fake_abalone2["Rings"].apply(lambda x:"K" if x<10)
File "", line 1 fake_abalone2["sex"]=fake_abalone2["Rings"].apply(lambda x:"K" if x<10)
SyntaxError: invalid syntax
You can use Python numpy instead of lambda function.
Import python numpy using import numpy as np
then you can use the following method to replace the string.
fake_abalone2['Sex'] = np.where(fake_abalone2['Rings']<10, 'K', fake_abalone2['Sex'])