How to use else in np.select function

Viewed 1229

I am categorizing a variable into a new variable. However I am unable to use the else function within my script:

df['cod_cat'] = np.select([
(df['cod'] >= 223101) & (df['cod'] <= 223199),
(df['cod'] >= 223201) & (df['cod'] <= 223299),
(df['cod'] >= 223301) & (df['cod'] <= 223399),
(df['cod'] >= 223401) & (df['cod'] <= 223499),
(df['cod'] >= 223501) & (df['cod'] <= 223599),
(df['cod'] == 223605)],[1,2,3,4,5,6)

In the end I would like to put the attribute else = 99.

Is there any way to do this using the format I used?

1 Answers

Add third parameter for default value:

df['cod_cat'] = np.select([
(df['cod'] >= 223101) & (df['cod'] <= 223199),
(df['cod'] >= 223201) & (df['cod'] <= 223299),
(df['cod'] >= 223301) & (df['cod'] <= 223399),
(df['cod'] >= 223401) & (df['cod'] <= 223499),
(df['cod'] >= 223501) & (df['cod'] <= 223599),
(df['cod'] == 223605)], [1,2,3,4,5,6], default=99)
Related