0

I have 3 columns. I want to update the third column, status, if the first column, a is greater than or equal to column b + 0.50. I've tried this following code but cannot get it to work:

if df['a'] >= (df['b'] + 0.50):
    df['status'] = 1

status is already populated with zeros.

how would i go about this?

worldCurrencies
  • 417
  • 3
  • 13
  • 2
    Pandas can do a lot of the work for you. `df.loc[df['a'] >= df['b']+0.50, 'status'] = 1` – mitoRibo May 25 '22 at 01:32
  • 2
    Keep in mind that code like `(df['b'] + 0.50)` will operate on *the entire column at once*. It creates a *new column* with all the modified values. Similarly with comparisons between two columns. A column cannot be used for an `if` check; see https://stackoverflow.com/questions/36921951 . But even then, `df['status'] = 1` sets *the entire column at once*. To write good Pandas code, it is necessary to understand this kind of "broadcasting" or "vectorization". You may find it useful to practice with Numpy first. – Karl Knechtel May 25 '22 at 01:48

2 Answers2

2

You can try with np.where

df['status'] = np.where(df['a'] >= (df['b'] + 0.50),1,0)

Or just

df['status'] = (df['a'] >= (df['b'] + 0.50)).astype(int)

Or loc

df.loc[(df['a'] >= (df['b'] + 0.50)),'status'] = 1
BENY
  • 317,841
  • 20
  • 164
  • 234
0

You can use lambda function :

def myFunc(x): 
    if(x[0]>=x[1]+0.50) :
        return 1
    else: return x[2]

df['status'] = df.apply(lambda x:myFunc(x),axis=1)