Python Equivalent of ifelse in R

30 Aug 2018

When you teach people, you learn!

Look at things in a different way, from another perspective. And you become much more efficient!

The most efficient way to do an ifelse ala Excel:

In R, it would be:


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

In Python, it would be:


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

check the full post out at stackoverflow: https://stackoverflow.com/questions/43711020/python-version-of-rs-ifelse-statement/43711056#43711056