Home >Database >Mysql Tutorial >How to Replicate SQL's DENSE_RANK() Function in Pandas?
Replicating SQL's DENSE_RANK() in Pandas DataFrames
Pandas offers a straightforward way to mimic SQL's DENSE_RANK()
function, which assigns ranks without gaps even when encountering tied values. This is crucial for scenarios requiring consecutive rank assignments regardless of duplicates.
Leveraging Pandas' rank()
Method
The core solution lies in Pandas' rank()
method. By specifying the method='dense'
argument, we achieve the desired dense ranking behavior.
Illustrative Example:
Let's consider a sample DataFrame:
<code class="language-python">import pandas as pd data = {'Year': [2012, 2013, 2013, 2014], 'Value': [10, 20, 25, 30]} df = pd.DataFrame(data)</code>
To generate a 'Rank' column mirroring DENSE_RANK()
, use this code:
<code class="language-python">df['Rank'] = df['Year'].rank(method='dense').astype(int) print(df)</code>
This produces the following output:
<code> Year Value Rank 0 2012 10 1 1 2013 20 2 2 2013 25 2 3 2014 30 3</code>
Notice how the tied 'Year' values (2013) receive the same rank (2), maintaining the dense ranking sequence. The .astype(int)
converts the rank to an integer data type for cleaner presentation.
The above is the detailed content of How to Replicate SQL's DENSE_RANK() Function in Pandas?. For more information, please follow other related articles on the PHP Chinese website!