0

I have a dataframe df where there are 3 columns

id code  status   
1  US    Y      
2  IN    Y
3  UK    Y
4  CN    Y
5  KR    Y 

I want to update column status to N where code not in ("US", "UK")

I tried using this but failed

df.loc[df['code'] not in ("US","UK"),["status"]] ='N'
Sociopath
  • 13,068
  • 19
  • 47
  • 75
priya
  • 67
  • 8
  • Possible duplicate of [Pandas: How do I assign values based on multiple conditions for existing columns?](https://stackoverflow.com/questions/30631841/pandas-how-do-i-assign-values-based-on-multiple-conditions-for-existing-columns) – Mohit Motwani Aug 10 '18 at 06:49

2 Answers2

1

You need:

df['status'] = np.where(df['code'].isin(["US","UK"]), df['status'], 'N')
Sociopath
  • 13,068
  • 19
  • 47
  • 75
1
df['status'] = df.apply(lambda x: 'N' if x[1] not in ['US','UK'] else x[2],axis=1)
Onyambu
  • 67,392
  • 3
  • 24
  • 53