15

I'm dealing with pandas dataframe and have a frame like this:

Year Value  
2012  10
2013  20
2013  25
2014  30

I want to make an equialent to DENSE_RANK () over (order by year) function. to make an additional column like this:

    Year Value Rank
    2012  10    1
    2013  20    2
    2013  25    2
    2014  30    3

How can it be done in pandas?

Thanks!

4 Answers 4

31

Use pd.Series.rank with method='dense'

df['Rank'] = df.Year.rank(method='dense').astype(int)

df

enter image description here

12

The fastest solution is factorize:

df['Rank'] = pd.factorize(df.Year)[0] + 1

Timings:

#len(df)=40k
df = pd.concat([df]*10000).reset_index(drop=True)

In [13]: %timeit df['Rank'] = df.Year.rank(method='dense').astype(int)
1000 loops, best of 3: 1.55 ms per loop

In [14]: %timeit df['Rank1'] = df.Year.astype('category').cat.codes + 1
1000 loops, best of 3: 1.22 ms per loop

In [15]: %timeit df['Rank2'] = pd.factorize(df.Year)[0] + 1
1000 loops, best of 3: 737 µs per loop
5
  • Note that you will want to use sort=True in the call to factorize, which will impact your timings as well (in my randomly generated 3M large numerical df, method 1, i.e. using the rank method turns out to be the fastest). The reason you assumed it works, is because the array's non-duplicate elements were already sorted.
    – Oliver W.
    May 16, 2017 at 13:18
  • Yes, but it depends if data are sort or not. In sample are sorted, so not necessary.
    – jezrael
    May 16, 2017 at 13:19
  • 1
    Indeed, and that's what I said. Because it's sorted, factorize will be faster. In general, data is not sorted and so factorize and rank will return different answers. I added the comment as a warning to future readers, who would blindly take over solutions without checking the conditions under which they're assumed to work.
    – Oliver W.
    May 16, 2017 at 13:42
  • @OliverW. - Thank you.
    – jezrael
    May 16, 2017 at 13:43
  • @piRSquared - Thanks, it hapens. Your solution was upvoted by me ;)
    – jezrael
    Feb 18, 2018 at 6:29
8

You can convert the year to categoricals and then take their codes (adding one because they are zero indexed and you wanted the initial value to start with one per your example).

df['Rank'] = df.Year.astype('category').cat.codes + 1

>>> df
   Year  Value  Rank
0  2012     10     1
1  2013     20     2
2  2013     25     2
3  2014     30     3
3

Groupby.ngroup

Will sort keys by default so smaller years get labeled lower. Can set sort=False to rank groups based on order of occurrence.

df['Rank'] = df.groupby('Year', sort=True).ngroup()+1

np.unique

Also sorts, so use return_inverse to rank the smaller values lowest.

df['Rank'] = np.unique(df['Year'], return_inverse=True)[1]+1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.