Reputation: 431
I have 2 pandas dataframes df_x and df_y and I want to update a ‘SCORE’ column that they both have in common. I want to update the score column with a score of 100 if the following conditions are met:
My trial below did not work. Any inputs?
if df_x.AGE_DAYS <= 45 and (df_x.BANNED != 1 or df_x.CHARGEBACK != 1):
df_x['SCORE'] = 100
if df_y.AGE_DAYS <= 45 and (df_y.BANNED != 1 or df_y.CHARGEBACK != 1):
df_y['SCORE'] = 100
Outcome: Basically, the 'Score' column should update the existing value with a 100 or do nothing at all if the above described criteria are met.
To set up a sample testing enviornment:
import pandas as pd
df = pd.DataFrame([[1, 1, 0], [100, 0,0], [46, 1, 0]], columns=['AGE_DAYS', 'Banned', 'Chargeback'])
print(df)
Desired output: Updated score column added to show that score values outside the criteria specified not changed. Only values changed if they meet the criteria of this search!
AGE BANNED CHARGEBACK SCORE "UPDATED SCORE"
45 1 0 75 75
33 0 0 45 **100**
44 0 0 77 **100**
235 0 1 75 75
43 1 0 88 88
21 0 0 23 **100**
1 0 0 56 **100**
432 1 1 12 12
Upvotes: 0
Views: 86
Reputation: 24314
Try:
c=(df['AGE']<=45) & df[['BANNED','CHARGEBACK']].ne(1).all(1)
#OR(both conditions are same so choose any one)
c=(((df['BANNED'].ne(1)) & (df['CHARGEBACK'].ne(1)))) & (df['AGE']<=45)
#your condition
#also notice that you need & in place of | in your condition that you posted in question
Finally use mask()
method:
df['UPDATED SCORE']=df['SCORE'].mask(c,100)
OR
you can also use numpy's where()
method for this:
#import numpy as np
df['UPDATED SCORE']=np.where(c,100,df['SCORE'])
Upvotes: 1
Reputation: 1486
you can try using apply
df_x['SCORE'] = df_x.apply(lambda x:100 if x['AGE_DAYS'] <= 45 and (x['BANNED'] != 1 or x['CHARGEBACK'] != 1)
df_y['SCORE'] = df_y.apply(lambda x: 100 if x['AGE_DAYS'] <= 45 and (x['BANNED'] != 1 or x['CHARGEBACK'] != 1)
Upvotes: 0