['b','b','b','a','a','c','c']
numpy.unique gives
['a','b','c']
How can I get the original order preserved
['b','a','c']
Great answers. Bonus question. Why do none of these methods work with this dataset? http://www.uploadmb.com/dw.php?id=1364341573 Here's the question numpy sort wierd behavior
Numpy np.unique()
is slow, O(Nlog(N)), but you can do this by following code:
import numpy as np
a = np.array(['b','b','b','a','a','c','c'])
_, idx = np.unique(a, return_index=True)
print(a[np.sort(idx)])
Output:
['b' 'a' 'c']
Pandas pd.unique()
is much faster for big array O(N):
import pandas as pd
a = np.random.randint(0, 1000, 10000)
%timeit np.unique(a)
%timeit pd.unique(a)
1000 loops, best of 3: 644 us per loop
10000 loops, best of 3: 144 us per loop
Note: Pandas pd.unique()
has the further benefit of preserving order by default:
Return unique values based on a hash table.
Uniques are returned in order of appearance. This does NOT sort.
Significantly faster than numpy.unique for long enough sequences. Includes NA values.