How to Turn 1 and 0 into True and False in pandas
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
frame[column] = frame[column].astype(bool)
| Part | What 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:

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))
uv run python int_to_bool.py
0 True
1 False
2 False
3 True
4 True
Name: subscribed, dtype: bool
0 False
1 True
2 True
Name: visits, dtype: bool

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:
import pandas as pd
visits = pd.Series([1.0, 0.0, None], name="visits")
print(visits)
print()
print(visits.astype(bool))
uv run python missing_to_bool.py
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
| Mistake | What happens | Do this instead |
|---|---|---|
| Converting a column with other numbers in it | Every non-zero value becomes True | Check the column holds only 1 and 0 |
| Converting a column with gaps | NaN becomes True | Fill or drop the gaps first |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
More True values than 1s | Other non-zero numbers, or missing values | Look 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
- pandas.Series.astype — converting a column to another type.
