Skip to main content

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

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

A column of True and False often needs to be 1 and 0: many machine learning tools expect a yes/no answer as numbers. In pandas, one method does it: .astype(int). This guide converts a column, shows what happens when the values are text instead of booleans, and counts the True values without converting, with real output from pandas 3.1.1.

Syntax​

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

Step 1: Convert the Column​

The example is three orders, with a paid column of booleans:

The bool_to_int.py script in a macOS-style code window: a DataFrame of three orders with a paid column of True, False, True, which is converted with astype(int) and printed with its column types.

bool_to_int.py
import pandas as pd

orders = pd.DataFrame({"order": [1001, 1002, 1003], "paid": [True, False, True]})
orders["paid"] = orders["paid"].astype(int)
print(orders)
print(orders.dtypes)
Terminal
uv run python bool_to_int.py
Output
order paid
0 1001 1
1 1002 0
2 1003 1
order int64
paid int64
dtype: object

The output of bool_to_int.py in a terminal: the paid column now holds 1, 0, 1, and both columns are int64.

paid is now int64: 1 for the two paid orders, 0 for the other.

Step 2: Watch Out for Text That Looks Like a Boolean​

A column read from a file can hold the words "True" and "False" instead of booleans. astype(int) refuses those:

bool_checks.py
import pandas as pd

orders = pd.DataFrame({"paid": [True, False, True]})
print("paid orders:", orders["paid"].sum())

text = pd.Series(["True", "False", "True"])
try:
text.astype(int)
except ValueError as error:
print("ValueError:", error)
print(text.eq("True").astype(int).tolist())
Terminal
uv run python bool_checks.py
Output
paid orders: 2
ValueError: invalid literal for int() with base 10: 'True'
[1, 0, 1]
  • ValueError: invalid literal for int() with base 10: 'True': text has to become a boolean first. .eq("True") compares each value with the word and gives booleans, which astype(int) then turns into 1 and 0. How to Turn yes and no into 1 and 0 in pandas does the same with other words.
  • paid orders: 2: to count the True values, you don't need to convert at all. .sum() counts True as 1.

Common Mistakes​

MistakeWhat happensDo this instead
astype(int) on the words "True" and "False"ValueError: invalid literal for int() with base 10: 'True'.eq("True").astype(int)
Converting only to count the True valuesAn extra step.sum() on the boolean column

Troubleshooting​

SymptomLikely causeFix
ValueError: invalid literal for int() with base 10: 'True'The column holds text, not booleansCompare with .eq("True") first

FAQ​

Does pandas read True and False from a CSV file as booleans?​

Yes, when the whole column holds them: reading a CSV from a zip gave a bool column for True and False.

Can I keep the column as booleans instead?​

Yes. Convert only when the tool you hand the data to wants numbers.

Conclusion​

orders["paid"].astype(int) turned True and False into 1 and 0. Text needs .eq("True") first, and counting needs no conversion at all.

Additional Resources​