Converting from a string to boolean in Python
Really, you just compare the string to whatever you expect to accept as representing true, so you can do this:
s == 'True'
Or to checks against a whole bunch of values:
s.lower() in ['true', '1', 't', 'y', 'yes', 'yeah', 'yup', 'certainly', 'uh-huh']
Be cautious when using the following:
>>> bool("foo")
True
>>> bool("")
False
Empty strings evaluate to False
, but everything else evaluates to True
. So this should not be used for any kind of parsing purposes.
How to convert string to Boolean in Python
You must be looking for eval(string)
>>> eval("3>1>5")
False
Convert True/False value read from file to boolean
bool('True')
and bool('False')
always return True
because strings 'True' and 'False' are not empty.
To quote a great man (and Python documentation):
5.1. Truth Value Testing
Any object can be tested for truth value, for use in an if or while
condition or as operand of the Boolean operations below. The
following values are considered false:
- …
- zero of any numeric type, for example,
0
,0L
,0.0
,0j
.- any empty sequence, for example,
''
,()
,[]
.- …
All other values are considered true — so objects of many types
are always true.
The built-in bool
function uses the standard truth testing procedure. That's why you're always getting True
.
To convert a string to boolean you need to do something like this:
def str_to_bool(s):
if s == 'True':
return True
elif s == 'False':
return False
else:
raise ValueError # evil ValueError that doesn't tell you what the wrong value was
Converting strings to boolean gives only False value
You can do this for both columns via regular expressions and df.replace
:
df.astype(str).replace({'(?i)True|TBC': True, '(?i)False|nan': False}, regex=True)
Col1 Col2
0 True True
1 True True
2 False False
3 False False
4 False True
5 True True
The pattern is case insensitive.
How can I convert String to Boolean in python?
You can use the eval
function to test the condition and then the str
function to go back:
>>> condition = 'True and False'
>>> test = eval(condition)
>>> test
False
>>> type(test)
<class 'bool'>
>>> result = str(test)
>>> result
'False'
>>> type(result)
<class 'str'>
What is the most Pythonic way to convert 0 and 1 strings to boolean?
There are many ways, but I would like to propose the following:
{'0': False, '1': True}[input_value]
This has the advantage of raising an exception if you ever get a value different to what you expect (because of a bug, a malfunction, an API change etc).
All the other options presented thus far will silently accept any string as input.
Converting from a string to boolean in Python
Really, you just compare the string to whatever you expect to accept as representing true, so you can do this:
s == 'True'
Or to checks against a whole bunch of values:
s.lower() in ['true', '1', 't', 'y', 'yes', 'yeah', 'yup', 'certainly', 'uh-huh']
Be cautious when using the following:
>>> bool("foo")
True
>>> bool("")
False
Empty strings evaluate to False
, but everything else evaluates to True
. So this should not be used for any kind of parsing purposes.
How to convert a column data type from 'string ' to 'boolean', preserving NaN?
After a comment by Stef I changed completely my answer:
If you have your colum as string 'True' or 'False' intermixed with
NaN values, you can use replace with a dictionary:
- replace string 'True' with boolean True,
- replace string 'False' with boolean False.
Something like:
ri_df.contraband_weapon.replace({'True': True, 'False': False}, inplace=True)
So the code can be quite short.
But the bad news is that the type of this column is still object.
The reason is that:
- most values are of bool type,
- but some of them are NaN, which is actually a special case of float.
Hence, there is no any "single" type among values in this column,
so the type can not be bool.
Edit following the question about "workaround"
I see that you want to preserve the "three value logic" (True / False / Unknown).
If you want to stay with native Pandas data types, I think there is no workaround, because:
- bool is either True or False (not third option as "unknown"),
- NaN is a special case of float,
so you have to live with this "mixture of types".
Maybe some alternative is to define a Categorical type, including three
categories, corresponding to True, False and Unknown and tranlate
each source value to a respective category.
Then there will be a single data type, but the dowside is that if you
want to have any "3-value bool operators / functions", you have to program
them on your own.
Related Topics
How to Make Separator in Pandas Read_CSV More Flexible Wrt Whitespace, for Irregular Separators
Why am I Getting "Indentationerror: Expected an Indented Block"
Understanding Python Unicode and Linux Terminal
Why Does Pyimport_Import Fail to Load a Module from the Current Directory
Pandas Add Column to Groupby Dataframe
Name This Python/Ruby Language Construct (Using Array Values to Satisfy Function Parameters)
Numpy Selecting Specific Column Index Per Row by Using a List of Indexes
How I Open Remote Server Folder Using Python
Assign Environment Variables from Bash Script to Current Session from Python
Sharing a Result Queue Among Several Processes
How to Create a Reference to a Variable in Python
Detecting When a Child Process Is Waiting for Input
Importerror: Libcblas.So.3: Cannot Open Shared Object File: No Such File or Directory
Changing the Formatting of a Datetime Axis in Matplotlib