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>Given:</p>

<pre><code>&gt;&gt;&gt; b
[['1' 'a']
 ['2' 'b']
 ['3' 'a']
 ['1' 'a']
 ['5' 'c']
 ['6' 'b']
 ['3' 'a']]
</code></pre>

<p>You can do:</p>

<pre><code>&gt;&gt;&gt; np.vstack({tuple(e) for e in b})
[['3' 'a']
 ['1' 'a']
 ['2' 'b']
 ['6' 'b']
 ['5' 'c']]
</code></pre>

<p>Since that is a set comprehension, you loose the order of the original. </p>

<p>Or, to maintain order, you can do: </p>

<pre><code>&gt;&gt;&gt; c = np.ascontiguousarray(b).view(np.dtype((np.void, b.dtype.itemsize * b.shape[1])))
&gt;&gt;&gt; b[np.unique(c, return_index=True)[1]]
[['1' 'a']
 ['2' 'b']
 ['3' 'a']
 ['5' 'c']
 ['6' 'b']]
</code></pre>

<p>Or, if you can use Pandas, this is really easy. Given the following DataFrame:</p>

<pre><code>&gt;&gt;&gt; df
  id  time
0  a     1
1  b     2
2  a     3
3  a     1
4  c     5
5  b     6
6  a     3
</code></pre>

<p>Just use <code>drop_duplicates()</code>:</p>

<pre><code>&gt;&gt;&gt; df.drop_duplicates()
  id  time
0  a     1
1  b     2
2  a     3
4  c     5
5  b     6
</code></pre>

python arrays numpy