0
I’m trying to convert a column into a data set where there is 'negative' and 'positive' for binaries or numeric items like 0 and 1, but I don’t know if I’m doing it right with the Pandas library.
pd.cut(data.Class, bins=['negative','positive'],labels=['0','1'])
but the following error appears:
ValueError: could not convert string to float: 'negative'
I have tried by Boolean Mask as follows:
mask = data['Class'] == 'negative'
data.loc[mask, 'Class'] = 0
data.loc[~mask, 'Class'] = 1
But then he turns them all into one of the guys!
A sample of the database I’m using is:
v8 v9 Class
0 0.00000 0.1224 negative
1 0.00000 0.0000 negative
2 0.00000 0.0000 negative
3 0.00000 0.0000 negative
4 0.00000 0.0561 negative
.. ... ... ...
166 0.66150 0.0000 negative
167 1.06155 0.0000 negative
168 1.62855 0.0000 negative
169 1.71045 0.0000 positive
170 1.54980 0.0000 positive
I would like the result to be as follows:
v8 v9 Class
0 0.00000 0.1224 0
1 0.00000 0.0000 0
2 0.00000 0.0000 0
3 0.00000 0.0000 0
4 0.00000 0.0561 0
.. ... ... ...
166 0.66150 0.0000 0
167 1.06155 0.0000 0
168 1.62855 0.0000 0
169 1.71045 0.0000 1
170 1.54980 0.0000 1
This is a conversion of a categorical variable into numerical. I didn’t find a question about this for pandas, so I answered your question. But it would be interesting to change the title to better reflect the content of the question. See the case of this analogous question for R: https://answall.com/questions/41889/converter-variables-qualitatives-em-factores-no-r
– Lucas