Reputation: 785
I have an array/pandas row:
array = [0.8, np.nan, 0.1, -0.5, 0.7]
I want this output:
array = [1, np.nan, 3, 4, 2]
These methods are ranking in the wrong direction for me:
scipy.stats.mstats.rankdata
scipy.stats.rankdata
Upvotes: 3
Views: 2564
Reputation:
Since you mentioned Pandas, you can use Series.rank method:
arr = [0.8, np.nan, 0.1, -0.5, 0.7]
pd.Series(arr).rank(ascending=False)
Out:
0 1.0
1 NaN
2 3.0
3 4.0
4 2.0
dtype: float64
This creates and returns a Pandas Series. If you want to avoid creating a Series, as @ajcr noted in the comments, you can use the rank function. This returns an ndarray:
pd.algos.rank_1d_float64(arr, ascending=False)
Out: array([ 1., nan, 3., 4., 2.])
Upvotes: 3
Reputation: 221574
Here's an approach -
mask = ~np.isnan(a)
out = np.full(a.size,np.nan)
out[mask] = np.unique(-a[mask],return_inverse=1)[1]+1
Sample run -
In [48]: a
Out[48]: array([ 0.8, nan, 0.1, -0.5, nan, 0.7])
In [49]: out
Out[49]: array([ 1., nan, 3., 4., nan, 2.])
Upvotes: 1