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

Filtering pandas with multiple conditions in a better way

I would like to assign names for the new column "SubtestName" based on the "SubtestID" column.

Currently my codes are as follows:

conditions = [(df4['subtestID'] == 325)|(df4['subtestID'] == 341)|(df4['subtestID'] == 1164)|(df4['subtestID'] == 1200),
              (df4['subtestID'] == 347)|(df4['subtestID'] == 357)|(df4['subtestID'] == 1308)|(df4['subtestID'] == 1330),
              (df4['subtestID'] == 328)|(df4['subtestID'] == 344)|(df4['subtestID'] == 1167)|(df4['subtestID'] == 1203)]

values = ["TestName1","TestName2","TestName3"]

df4['subTestName'] = np.select(conditions, values)

I would like to rewrite my codes in a better way without repeating "df4[‘subtestID’]" every time I want to assign a new ID. I am planning to assign another 30 subtestnames.

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 tried using this way but it gave me an error.

df4['subtestID'] in (325,341,1164,1200) 

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()

Any other methods I can use to assign the names to the SubtestID?

>Solution :

You can simply us isin() as follows:

df4['subtestID'].isin([325,341,1164,1200]) 
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