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.
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.
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With