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.
astype() method is used to cast a pandas object to a specified dtype. astype() function also provides the capability to convert any suitable existing column to categorical type.
Pandas DataFrame astype() Method The astype() method returns a new DataFrame where the data types has been changed to the specified type.
astype() is used to do such data type conversions. copy: Makes a copy of dataframe/series. errors: Error raising on conversion to invalid data type. For example dict to string.
To convert boolean to integer in python, we will use int(bool) and then it will be converted to integer.
Next, the astype () method is used to convert the entire dataframe into a float type from a int type element. so at the end of astype () process the entire core dataframe is converted into a float type and named as transformed dataframe. the contents of the transformed datatype is printed onto the console along with the type of it.
Casting is the process of converting entity of one data type into a different data type. So when a entity like object or variable gets casted it will be transformed from its source type to a different type. In pandas this conversion process can be achieved by means of the astype () method.
Your values in list are strings not boolean. Your list contains strings, and string converts to False only when it is empty. There is problem strings values are converted to Trues.
The python type or a numpy.dtype can be mentioned here which will make the whole object to be of one specific object type. Additionally {col: dtype.. . . . . . . . . } can be used to convert one or more columns of the object to some specific type.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With