22

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.

JJPUCK
  • 225
  • 1
  • 2
  • 4

2 Answers2

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
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
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
Mohamed Thasin ah
  • 10,754
  • 11
  • 52
  • 111