Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can you have more than two responses to np.where

I have the following np.where statement

df['DCC'] = np.where((df['RxDate'] - df['ABdat']).dt.days > 0, 0, df['DCC']

If the difference between ABdat and RxDate > 0 then it assigns 0 to DCC if not DCC remains as it was.

If the difference between ABdat and RxDate > 0 I would like to assign 0 to DCC and assign another column AbCode == 1 on the same row.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I could write a second np.where statement however I was wondering if it was possible to incorporate multiple responses into np.where

For example

df['DCC'] = np.where((df['RxDate'] - df['ABdat']).dt.days > 0, (0 & df['AbCode']==1), df['DCC']

This did not give an error however it did not toggle AbCode to 1

>Solution :

I would do something like this:

mask = (df['RxDate'] - df['ABdat']).dt.days > 0
df.loc[mask, ['DCC', 'AbcCode']] = [0,1]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading