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
  • 16,580
  • 17
  • 88
  • 94
mark mcmurray
  • 1,581
  • 4
  • 18
  • 28

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
  • 61,779
  • 13
  • 130
  • 153