mardi 29 novembre 2016

Equivalent of "case" for np.where

np.where lets you pick values to assign for a boolean type query, e.g.

test = [0,1,2]
np.where(test==0,'True','False')
print test
['True',1,2]

Which is basically an 'if' statement. Is there a pythonic way of having an 'if, else if, else' kind of statement (with different cases) for a numpy array?

This is my workaround:

color = [0,1,2]
color = np.where(color==0,'red',color)
color = np.where(color==1,'blue',color)
color = np.where(color==2,'green',color)
print color
['red','blue','green']

But I wonder if there's a better way of doing this.

Aucun commentaire:

Enregistrer un commentaire