Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing positive(+) and negative(-) signs to integers in python

Tags:

python

pandas

I have a dataframe with the following column:

A
+
+
-
+
-

How do I convert this column into integer values. So that I could multiply it to other numerical values?

Therefore, all '+' would be replaced by 1 and '-' by -1.

like image 780
A.DS Avatar asked Nov 27 '22 16:11

A.DS


2 Answers

You can use:

df.A = df.A.map({'+': 1, '-': -1})
like image 82
llllllllll Avatar answered Nov 30 '22 04:11

llllllllll


If I understand correctly, you could just use replace:

>>> df
   A
0  +
1  +
2  -
3  +
4  -

new_df = df.replace({'A':{'+':1, '-':-1}})

>>> new_df
   A
0  1
1  1
2 -1
3  1
4 -1
like image 33
sacuL Avatar answered Nov 30 '22 06:11

sacuL