I have a dataset that contains records like :
@attribute pelvic_incidence numeric
@attribute pelvic_tilt numeric
@attribute lumbar_lordosis_angle numeric
@attribute sacral_slope numeric
@attribute pelvic_radius numeric
@attribute degree_spondylolisthesis numeric
@data
74.09473084,18.82372712,76.03215571,55.27100372,128.4057314,73.38821617,Abnormal
87.67908663,20.36561331,93.82241589,67.31347333,120.9448288,76.73062904,Abnormal
48.25991962,16.41746236,36.32913708,31.84245726,94.88233607,28.34379914,Abnormal
38.50527283,16.96429691,35.11281407,21.54097592,127.6328747,7.986683227,Normal
54.92085752,18.96842952,51.60145541,35.952428,125.8466462,2.001642472,Normal
44.36249017,8.945434892,46.90209626,35.41705528,129.220682,4.994195288,Normal
48.3189305,17.45212105,47.99999999,30.86680945,128.9803079,-0.910940567,Normal
I wish to create a `DataFrame' from the given dataset and then change the labels of the column named 'class', from 'Abnormal' to 0 and 'Normal' to 1 respectively. I did the following:
raw_data = loadarff('column_2C_weka.arff')
df = pd.DataFrame(raw_data[0])
df["class"].replace({"Abnormal": "0" , "Normal" : "1"},inplace = True)
print(df['class'])
Unfortunately, the column 'class' is not updating the values i.e. it still shows the same 'Abnormal' and 'Normal' data labels.
To be more sure about how the replace
method works, I tried the same with a small DataFrame:
df = pd.DataFrame({"column1": ["a", "b", "a"]})
print(df)
df["column1"].replace({"a": "x", "b": "y"}, inplace=True)
print(df)
Surprisingly, it does change the values from a to x and b to y:
column1
0 a
1 b
2 a
column1
0 x
1 y
2 x
I am baffled. Why is it not occurring with my dataset but gets replaced with this DataFrame?
Thanks in advance.
P. S : Something like this worked out for me
df['class'] = df['class'].astype(str).str.replace('Abnormal', '0')
I don't have a clue about how it got the desired output and not all the previous ones! Any help is appreciated.