Take Unique of numpy array according to 2 column values.
<p>I have Numpy array in python with two columns as follows : </p>

<pre><code>time,id
1,a
2,b
3,a
1,a
5,c
6,b
3,a
</code></pre>

<p>i want to take unique time of each user. 
For above data i want below output. </p>

<pre><code>time,id
1,a
2,b
3,a
5,c
6,b
</code></pre>

<p>That is, I want to take only unique rows. so, 1,a and 3,a will not repeat in the result. 
I have both the column as string datatype and have a very large 2-D array. 
one solution may be, i can iterate over all the rows and make a set. But that will be very slow. Please suggest an efficient way to implement it. </p>

<p>If you go back to your original list format data and create a structured array, then determining the unique values is much easier.</p>

<pre><code>a = [['1', 'a'], ['2', 'b'], ['3', 'a'],['1', 'a'],['5', 'c'], ['6', 'b'], ['3', 'a']]

tup = [tuple(i) for i in a]  # you need a list of tuples, a kludge for now

dt = [('f1', '&lt;U5'), ('f2', '&lt;U5')]  # specify a dtype with two columns

b = np.array(tup, dtype=dt)  # create the array with the dtype

np.unique(b)  # get the unique values
array([('1', 'a'), ('2', 'b'), ('3', 'a'), ('5', 'c'), ('6', 'b')], 
      dtype=[('f1', '&lt;U5'), ('f2', '&lt;U5')])

np.unique(b).tolist()  # and if you need a list, just change the array
[('1', 'a'), ('2', 'b'), ('3', 'a'), ('5', 'c'), ('6', 'b')]
</code></pre>

<p>Reference:  <a href="http://stackoverflow.com/questions/16970982/find-unique-rows-in-numpy-array">Find unique rows in numpy.array</a></p>

<p>A combination of Joe Kingston and Jaime recommendations deal with views and the above can be simplified to the following.  Nicely, this option relies on view, a change of dtype to a structured array and a slice into the original array using the indices of the unique values in the structured view.</p>

<pre><code>&gt;&gt;&gt; dt = a.dtype.descr * a.shape[1]
&gt;&gt;&gt; a_view = a.view(dt)
&gt;&gt;&gt; a_uniq, a_idx = np.unique(a_view, return_index=True)
&gt;&gt;&gt; a[a_idx]
array([['1', 'a'],
       ['2', 'b'],
       ['3', 'a'],
       ['5', 'c'],
       ['6', 'b']], 
      dtype='&lt;U1')
</code></pre>

python arrays numpy