The Python Oracle

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

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

--

Become part of the top 3% of the developers by applying to Toptal
https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game 5

--

Chapters
00:00 Question
00:45 Accepted answer (Score 11)
01:13 Answer 2 (Score 0)
01:29 Thank you

--

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

Accepted answer links:
[strings values are converted to Trues]: https://stackoverflow.com/a/715455

--

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