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>Use <code>pd.Series.rank</code> with <code>method='dense'</code></p>

<pre><code>df['Rank'] = df.Year.rank(method='dense').astype(int)

df
</code></pre>

<p><a href="http://i.stack.imgur.com/67n7I.png" rel="nofollow"><img src="http://i.stack.imgur.com/67n7I.png" alt="enter image description here"></a></p>

python sql pandas