Pandas DENSE RANK
<p>I'm dealing with pandas dataframe and have a frame like this:</p>

<pre><code>Year Value  
2012  10
2013  20
2013  25
2014  30
</code></pre>

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

<pre><code>    Year Value Rank
    2012  10    1
    2013  20    2
    2013  25    2
    2014  30    3
</code></pre>

<p>How can it be done in pandas?</p>

<p>Thanks!</p>

<p>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).</p>

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

&gt;&gt;&gt; df
   Year  Value  Rank
0  2012     10     1
1  2013     20     2
2  2013     25     2
3  2014     30     3
</code></pre>

python sql pandas