2

This is probably a really stupid question, but I have searched and cannot find an answer anywhere (probably because it's too stupid a question).

I have a 2D NumPy array with multiple columns. I want to identify unique elements in the 1st or 2nd column, but not in the rest of the columns:

array([['A', 'B', '3', '4'],
       ['C', 'D', '3', '5']], 
      dtype='|S1')

Using np.unique will get unique values in the array, and I can index a single column like so:

np.unique(example_array[:,0])
Out[16]: 
array(['A', 'C'], 
      dtype='|S1')

How can I index it so that I can find all the unique values in [;,0] and in [:,1]?

SherylHohman
  • 14,460
  • 16
  • 79
  • 88
mark mcmurray
  • 1,377
  • 3
  • 15
  • 26

1 Answers1

2

Use a :2 slice on the second dimension as well, to include more than one column.

np.unique(example_array[:,:2])
Out[]: 
array(['A', 'B', 'C', 'D'], 
      dtype='|S1')
shx2
  • 57,957
  • 11
  • 121
  • 147