Skip to main content

How to Turn yes and no into 1 and 0 in pandas

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
frame[column] = frame[column].eq("yes").astype(int)
PartWhat 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:

The yes_no.py script in a macOS-style code window: it builds a DataFrame of customers ana, ben, cho and dev with a subscribed column of yes, no, no, yes, replaces the column with subscribed.eq yes .astype int, and prints the table.

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)
Terminal
uv run python yes_no.py
Output
customer subscribed
0 ana 1
1 ben 0
2 cho 0
3 dev 1

A terminal running uv run python yes_no.py: the table now shows subscribed as 1 for ana and dev and 0 for ben and cho.

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:

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"}))
Terminal
uv run python yes_no_check.py
Output
[1, 0, 0, 0]
[1.0, 0.0, nan, nan]
['Yes', 'maybe']

A terminal running uv run python yes_no_check.py: eq gives 1, 0, 0, 0; map gives 1.0, 0.0, nan, nan; and the unexpected values are Yes and maybe.

  • eq counted Yes and maybe as "no" without a word.
  • map left them as nan, which shows something needs a decision.
  • The last line lists exactly which values are neither yes nor no.

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​

MistakeWhat happensDo this instead
Converting without looking at the valuesTypos and other answers silently become 0List the unexpected values first, as in Step 2
map(...).astype(int) when unknown values existIntCastingNaNError: Cannot convert non-finite values (NA or inf) to integerHandle the nan values first, or use eq once the column is clean

Troubleshooting​

SymptomLikely causeFix
More 0s than you expectedSome values differ in case or spelling from "yes".str.lower().eq("yes"), after listing the values
nan in the result of mapValues that aren't keys of the dictionaryAdd 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​