Yilun Zhang
Yilun Zhang

Reputation: 9018

Python: create category based on values in two arrays

Say I have two lists:

arrayA = np.array([3,4,1,2,5,6,8,6,3])
arrayB = np.array([4,2,5,6,1,3,6,5,3])

which basically represents a point in 2D.

I want to get a label list that looks like:

listLael = [type1,type2,type1,type2,...]

that have the same length as arrayA and arrayB and

type1 if arrayA value >= 5 and arrayB value >= 5
type2 if eith arrayA or arrayB value < 5

I know that I can go through both array and get it but is there are fast and convenient way of doing this with numpy array?

Upvotes: 0

Views: 376

Answers (1)

Ashwini Chaudhary
Ashwini Chaudhary

Reputation: 251096

Use numpy.where:

>>> np.where((arrayA >= 5) & (arrayB >= 5), 'type1', 'type2')
array(['type2', 'type2', 'type2', 'type2', 'type2', 'type2', 'type1',
       'type1', 'type2'],
      dtype='|S5')

Upvotes: 2

Related Questions