Cody
Cody

Reputation: 499

Pandas conversion from object to boolean always returns True using astype

I have a column in a dataframe that I need to convert from object to boolean. However, the value changes from False to True when using astype. Any ideas on how to prevent this?

df['isgood'] returns:

0    True
1    False
2    False

df['isgood'].astype(bool) returns:

0    True
1    True
2    True

Upvotes: 5

Views: 19259

Answers (1)

BENY
BENY

Reputation: 323226

The True and False in your df type is str, if you need convert it to bool type

df.isgood=='True'
Out[420]: 
0     True
1    False
2    False
Name: isgood, dtype: bool

Upvotes: 10

Related Questions