배운 것/Python

[numpy] numpy.select(conditionlist, choicelist, default=)

MOR1ATY 2021. 7. 20. 16:13

 

여기서는 새로운 column('PURPOSE')를 생성하기 위해 사용했다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
conditionlist = [
    (credit_df['NEW_CAR'] == 1),
    (credit_df['USED_CAR'] == 1),
    (credit_df['FURNITURE'] == 1),
    (credit_df['RADIO/TV'] == 1),
    (credit_df['EDUCATION'] == 1),
    (credit_df['RETRAINING'] == 1)]
 
choicelist = ['New Car', 'Used Car', 'Furniture', 'Radio/TV', 'Education', 'Retraining']
 
credit_df['PURPOSE'] = np.select(conditionlist, choicelist, default='Other')
 
credit_df.drop(['NEW_CAR', 'USED_CAR', 'FURNITURE', 'RADIO/TV', 'EDUCATION', 'RETRAINING'], axis=1, inplace=True)
 
credit_df.head()
cs

 

아래는 파라미터 설명

 

1. numpy.select(condlist, choicelist, default=0)

조건에 따라 선택 목록의 요소에서 가져온 배열을 반환.

 

2. Parameterscondlistlist of bool ndarrays

선택 목록의 어떤 배열에서 출력 요소를 가져 choicelist 를 결정하는 조건 목록. 여러 조건이 충족되면 condlist 에서 처음 발견된 조건 이 사용.

 

3. choicelistlist of ndarrays

출력 요소를 가져오는 배열 목록. condlist 와 길이가 같아야 함.

 

4. defaultscalar, optional

모든 조건이 False로 평가될 때 output 삽입된 요소.

 

5. Returnsoutputndarray

위치 m의 출력은 choicelist 에 있는 배열 의 m 번째 요소, condlist 에 있는 해당 배열의 m 번째 요소는 True.