The Python Oracle

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

--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Popsicle Puzzles

--

Chapters
00:00 Why Is .Astype('Bool') Converting All Values To True?
00:39 Accepted Answer Score 13
00:58 Answer 2 Score 0
01:09 Thank you

--

Full question
https://stackoverflow.com/questions/5208...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #pandas

#avk47



ACCEPTED ANSWER

Score 13


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



ANSWER 2

Score 0


one clear approach do this will be,

test=test[0]=='True'

O/P:

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