Skip to main content

How to Turn 1 and 0 into True and False in pandas

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

A yes/no answer stored as 1 and 0 sometimes has to become True and False, for a tool that tells classes by their type, such as MLflow's evaluation. In pandas, .astype(bool) does it. This guide converts a column, then shows the two values that turn into True without being 1, with real output from pandas 3.1.1.

Syntax​

Syntax
frame[column] = frame[column].astype(bool)
PartWhat it means
frame[column]A column of 1 and 0
.astype(bool)Returns the column as booleans: 0 becomes False, every other value True
frame[column] = ...Puts the converted column back in place of the old one

The same works on a Series on its own: series.astype(bool).

Step 1: Convert the Column​

The example is five customers, with a subscribed column of 1 and 0. The second part converts a column that holds a 2:

The int_to_bool.py script in a macOS-style code window: a Series subscribed of 1, 0, 0, 1, 1 converted with astype(bool) and printed, then a Series visits of 0, 1, 2 converted and printed the same way.

int_to_bool.py
import pandas as pd

subscribed = pd.Series([1, 0, 0, 1, 1], name="subscribed")
print(subscribed.astype(bool))
print()
visits = pd.Series([0, 1, 2], name="visits")
print(visits.astype(bool))
Terminal
uv run python int_to_bool.py
Output
0 True
1 False
2 False
3 True
4 True
Name: subscribed, dtype: bool

0 False
1 True
2 True
Name: visits, dtype: bool

The output of int_to_bool.py in a terminal: subscribed becomes True, False, False, True, True with dtype bool, and visits 0, 1, 2 becomes False, True, True.

subscribed became booleans. In visits, the 2 became True as well: astype(bool) turns every number other than 0 into True, so check that a column holds only 1 and 0 before you convert it.

Step 2: Watch Out for Missing Values​

A missing value isn't 0, so it isn't False either:

missing_to_bool.py
import pandas as pd

visits = pd.Series([1.0, 0.0, None], name="visits")
print(visits)
print()
print(visits.astype(bool))
Terminal
uv run python missing_to_bool.py
Output
0 1.0
1 0.0
2 NaN
Name: visits, dtype: float64

0 True
1 False
2 True
Name: visits, dtype: bool

The missing value, NaN, became True. Fill or drop missing values first, so a gap can't turn into a yes.

Common Mistakes​

MistakeWhat happensDo this instead
Converting a column with other numbers in itEvery non-zero value becomes TrueCheck the column holds only 1 and 0
Converting a column with gapsNaN becomes TrueFill or drop the gaps first

Troubleshooting​

SymptomLikely causeFix
More True values than 1sOther non-zero numbers, or missing valuesLook at column.value_counts(dropna=False) before converting

FAQ​

How do I go back from True and False to 1 and 0?​

With .astype(int): How to Turn True and False into 1 and 0 in pandas.

Why would a tool want booleans instead of 1 and 0?​

Some tools tell a yes/no answer from a number by its type. MLflow's model evaluation, for example, treats True and False as classes but warns about 1 and 0: Fixed: the model type looks like None.

Conclusion​

subscribed.astype(bool) turned 1 and 0 into True and False. Any other number, and a missing value, becomes True too, so a column should hold only 1 and 0 when you convert it.

Additional Resources​