Home > Database > Mysql Tutorial > How to Replicate SQL's DENSE_RANK() Function in Pandas?

How to Replicate SQL's DENSE_RANK() Function in Pandas?

DDD
Release: 2025-01-12 06:54:43
Original
293 people have browsed it

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:

import pandas as pd

data = {'Year': [2012, 2013, 2013, 2014], 'Value': [10, 20, 25, 30]}
df = pd.DataFrame(data)
Copy after login

To generate a 'Rank' column mirroring DENSE_RANK(), use this code:

df['Rank'] = df['Year'].rank(method='dense').astype(int)
print(df)
Copy after login

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>
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template