Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python version of R's ifelse statement

I am trying to learn Python after learning R and an simple ifelse statement.

In R I have:

df$X <- if(df$A == "-1"){-df$X}else{df$X}

But I am unsure how to implement it in Python, I have tried:

df['X'][df['A'] <1] = -[df['X']
df['X'][df['A'] >1] = [df['X']

But this leads to errors, would appreciate some help.

like image 333
DenJJ Avatar asked Apr 30 '17 20:04

DenJJ


2 Answers

The equivalent is np.where:

import numpy as np
np.where(df['A'] < 1, -df['X'], df['X'])

This checks if the values in column A are lower than 1. If so, it returns the corresponding value multiplied by -1 from df['X'], otherwise it returns the corresponding value in df['X'].

That said, your error/warning is probably raised because of chained indexing. Instead of df['X'][df['A'] <1] you should use df.loc[df['A'] <1, 'X']. Then you can do the same with two steps as you have shown in the question.

like image 138
ayhan Avatar answered Sep 21 '22 23:09

ayhan


It is also possible to use list comprehension for doing an equivalent of ifelse of R in Python. Showing an example in Python 3, with l and m as equivalents of df['A'] and df['X']

l = [ 1, 2, -3, -4, -5]
m = [ 10, 20, -30, -40, 50]

k = [ y if x>0 else -y for x,y in list(zip(l,m))]
k
>>> [10, 20, 30, 40, -50]

This removes the dependence on numpy

In addition, it can also be used for filtering out unnecessary values

k2 = [ y  for x,y in list(zip(l,m)) if x>0]
k2
>>>[10, 20]
like image 45
Tapajit Dey Avatar answered Sep 17 '22 23:09

Tapajit Dey