CS
[Numpy]np.select
프리랜서를꿈꾸는자
2020. 10. 19. 10:52
728x90
np.select(condition_list, choice_list, default)
x = np.arange(10)
>>> condlist = [x<3, x>5]
>>> choicelist = [x, x**2]
>>> np.select(condlist, choicelist)
array([ 0, 1, 2, 0, 0, 0, 36, 49, 64, 81])
-condition_list에서 여러개 condition을 만족한다면 first encounter condition에 해당하는 choice를 적용함
-default는 어떠한 condition 도 만족하지 않는 경우 output value
ex)
condlist = [
(score_df['average'] >= 90),
(score_df['average'] < 90) & (score_df['average'] >= 80),
(score_df['average'] < 80) & (score_df['average'] >= 70)]
choicelist = ['A','B','C']
score_df['Grade'] = np.select(condlist, choicelist)
728x90