JJPUCK
JJPUCK

Reputation: 225

Why is .astype('bool') converting all values to True?

I'm trying to convert a column in a dataframe of strings 'TRUE' and 'FALSE' to a boolean. For other data types I've simply used astype and not had any issues, but for some reason using it on this column converts all the values to True.

I have repeated the test on a list of strings as follows

test = ['False','False','True']
test = pd.DataFrame(test)
test = test.astype('bool')

But it gives the same result, what is going on here and how do I properly convert the datatype? I've tried using map and replace to change the values before conversion but neither changed the outcome.

Upvotes: 22

Views: 23414

Answers (2)

Mohamed Thasin ah
Mohamed Thasin ah

Reputation: 11192

one clear approach do this will be,

test=test[0]=='True'

O/P:

0    False
1    False
2     True
Name: 0, dtype: bool
bool

Upvotes: 0

jezrael
jezrael

Reputation: 862661

There is problem strings values are converted to Trues.

Solution:

test = ['False','False','True']
test = pd.DataFrame(test)
test = test[0].map({'False':False, 'True':True})
print (test)
0    False
1    False
2     True
Name: 0, dtype: bool

Or:

import ast

test = test[0].map(ast.literal_eval)
print (test)
0    False
1    False
2     True
Name: 0, dtype: bool

Upvotes: 13

Related Questions