check and update column based on multiple values

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



check and update column based on multiple values



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")


code not in ("US", "UK")



I tried using this but failed


df.loc[df['code'] not in ("US","UK"),["status"]] ='N'





Possible duplicate of Pandas: How do I assign values based on multiple conditions for existing columns?
– Mohit Motwani
Aug 10 at 6:49




2 Answers
2



You need:


df['status'] = np.where(df['code'].isin(["US","UK"]), df['status'], 'N')


df['status'] = df.apply(lambda x: 'N' if x[1] not in ['US','UK'] else x[2],axis=1)






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard