How to Turn yes and no into 1 and 0 in pandas
Survey answers, sign-ups and many dataset labels arrive as words, yes and no, while machine learning models expect numbers. In pandas, .eq("yes").astype(int) turns a yes/no column into 1 and 0 in one line. This guide converts a small table of answers, then shows why to check for values that are neither, with real output from pandas 3.1.1.
Syntax
frame[column] = frame[column].eq("yes").astype(int)
| Part | What it means |
|---|---|
frame[column].eq("yes") | Compares every value with "yes": True where it is, False everywhere else |
.astype(int) | Turns True into 1 and False into 0 |
frame[column] = ... | Replaces the column with the numbers |
Step 1: Convert a Column
The example is four customers and whether they subscribed. Create yes_no.py:

import pandas as pd
answers = pd.DataFrame(
{"customer": ["ana", "ben", "cho", "dev"], "subscribed": ["yes", "no", "no", "yes"]}
)
answers["subscribed"] = answers["subscribed"].eq("yes").astype(int)
print(answers)
uv run python yes_no.py
customer subscribed
0 ana 1
1 ben 0
2 cho 0
3 dev 1

Step 2: Watch for Values That Are Neither
.eq("yes") turns anything that isn't exactly "yes" into 0, including "Yes" with a capital letter and answers like "maybe". map with a dictionary is stricter: values it doesn't know become NaN, "not a number", so you can see them. Create yes_no_check.py:
import pandas as pd
answers = pd.Series(["yes", "no", "Yes", "maybe"])
print(answers.eq("yes").astype(int).tolist())
print(answers.map({"yes": 1, "no": 0}).tolist())
print(sorted(set(answers) - {"yes", "no"}))
uv run python yes_no_check.py
[1, 0, 0, 0]
[1.0, 0.0, nan, nan]
['Yes', 'maybe']

eqcountedYesandmaybeas "no" without a word.mapleft them asnan, which shows something needs a decision.- The last line lists exactly which values are neither
yesnorno.
Check your column this way once. If it really only holds yes and no, .eq("yes").astype(int) is safe. If capital letters sneak in, compare in lowercase: .str.lower().eq("yes") turns ["yes", "no", "Yes"] into [1, 0, 1].
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
| Converting without looking at the values | Typos and other answers silently become 0 | List the unexpected values first, as in Step 2 |
map(...).astype(int) when unknown values exist | IntCastingNaNError: Cannot convert non-finite values (NA or inf) to integer | Handle the nan values first, or use eq once the column is clean |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| More 0s than you expected | Some values differ in case or spelling from "yes" | .str.lower().eq("yes"), after listing the values |
nan in the result of map | Values that aren't keys of the dictionary | Add them to the dictionary, or fix the data |
FAQ
Why int and not bool?
1 and 0 work anywhere numbers do, and they add up to a count: answers["subscribed"].sum() is the number of yeses.
Which one is 1?
The answer you're predicting, usually "yes". scikit-learn treats 1 as the positive class by default in metrics such as precision and recall.
Conclusion
.eq("yes").astype(int) turned the subscribed column into 1 and 0. Before converting real data, list the values that are neither yes nor no: eq quietly counts them as 0, while map shows them as nan.
Additional Resources
- pandas.Series.eq — element-wise comparison.
- pandas.Series.map — mapping values with a dictionary.
