Skip to content

Instantly share code, notes, and snippets.

@christinebuckler
Last active November 27, 2023 19:05
Show Gist options
  • Save christinebuckler/0169083e6257e3160d673143c9f78b35 to your computer and use it in GitHub Desktop.
Save christinebuckler/0169083e6257e3160d673143c9f78b35 to your computer and use it in GitHub Desktop.
how to use np select
np.select(condlist, choicelist, default=0)
# EXAMPLE
CondList = [
(data['FBG'] >= 126) | (data['HbA1c'] >= 6.5),
((data['FBG'] < 126) & (data['FBG'] >= 100)) | ((data['HbA1c'] < 6.5) & (data['HbA1c'] >= 5.7)),
(data['FBG'] < 100) | (data['HbA1c'] < 5.7)]
ChoiceList = [2,1,0]
data.insert(1,'DM',np.select(CondList,ChoiceList,default=3))
# https://stackoverflow.com/questions/71380788/is-it-possible-to-use-numpy-select-with-a-dataframe-where-the-choicelist-depen
# https://stackoverflow.com/questions/39109045/numpy-where-with-multiple-conditions
# https://stackoverflow.com/questions/45080003/speed-up-python-apply-row-wise-functions
# https://stackoverflow.com/questions/23586510/return-multiple-columns-from-pandas-apply
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment