I am just new in Python, and I am trying to do an analysis. But before that, I want to recode some of the variables. I am wondering if this R code has an equivalent in Python.
df$col1 <- ifelse(df$col1 == "yes", 1, 0)
Here df
is a pandas.DataFrame
and col1
is one of its columns.
One possible option - probably the most similar to R's ifelse
in terms of syntax - is to use the where
function from numpy
import numpy as np
df['col1'] = np.where(df['col1'] == "Yes", 1, 0)