Reputation: 115
I have a text file and trying to process it. at the end I have to convert one of the variables from str
to int
.
if I print the variable using this command:
print(y_test.iloc[:,1])
I would get the following results:
0 B
1 B
2 A
3 A
4 A
5 A
6 A
7 A
8 B
9 B
10 B
11 B
12 B
13 B
Name: group, dtype: object
and if I get the type of this variable:
print(type(y_test.iloc[:,1]))
I would get this output:
<class 'pandas.core.series.Series'>
but my expected output is this (A to 0 and B to 1):
0 0
1 0
2 1
3 1
4 1
5 1
6 1
7 1
8 0
9 0
10 0
11 0
12 0
13 0
to do so, I tried the following code:
y2 = int(y_test.iloc[:,1])
but did not return what I want. do you know how to do that?
Upvotes: 0
Views: 85
Reputation: 17814
You can use the method replace()
:
y_test.iloc[:, 1].replace({'B': 0, 'A': 1})
Upvotes: 0
Reputation: 317
To get A as 1 and B (or anything else) as 0 you can use the below. It converts all A's to True, which are then converted to the integer value of True, which is 1.
(y_test.iloc[:,1] == 'A').astype(int)
Upvotes: 2