Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

pandas

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.

like image 664
JJPUCK Avatar asked Aug 30 '18 05:08

JJPUCK


People also ask

What does Astype do in Python?

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.

What is Astype in Python pandas?

Pandas DataFrame astype() Method The astype() method returns a new DataFrame where the data types has been changed to the specified type.

What does Astype str do?

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.

How do you convert boolean to int in Python?

To convert boolean to integer in python, we will use int(bool) and then it will be converted to integer.

What is the use of astype in Dataframe?

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.

What is an astype in pandas?

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.

Why is my list not returning a boolean value?

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.

How to convert an object to a specific type in Python?

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.


1 Answers

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
like image 135
jezrael Avatar answered Oct 21 '22 17:10

jezrael