3

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.

Ric S
  • 9,073
  • 3
  • 25
  • 51
ambmil
  • 115
  • 3
  • 9
  • 1
    Python has not an actual command to declare variable and they are created in the first moment you assign a value to it in a similar fashion to `example = "A string of text"`. You can explore the following [guide](https://sites.google.com/site/aslugsguidetopython/data-analysis/pandas/calling-r-from-python) and this other [site](https://www.kdnuggets.com/2015/12/using-python-r-together.html) if you are interested in approaches to using both R and Python altogether. – Daniel Ocando Jul 07 '20 at 09:45
  • 1
    I do not know R, but I am assuming that `df` is a `pandas.DataFrame` object and your code essentially wants to convert the values of its `col1` column to 0 and 1. If that's the case, a fast way to do it is `df['col1'] = (df['col1'] == "yes")*1` . If you want, I can convert this to a proper answer in which I will explain what I did – kyriakosSt Jul 07 '20 at 09:46
  • @ambmil Was this what you wanted and were my assumptions correct? If so, should I convert it to an answer? – kyriakosSt Jul 07 '20 at 09:52
  • 1
    @kyriakosSt, yes... thank you – ambmil Jul 07 '20 at 10:16

1 Answers1

6

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)
Ric S
  • 9,073
  • 3
  • 25
  • 51