Home >Database >Mysql Tutorial >How Does Pandas `rank(method='dense')` Assign Unique Ranks to Tied Values?

How Does Pandas `rank(method='dense')` Assign Unique Ranks to Tied Values?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2025-01-12 07:53:43441browse

How Does Pandas `rank(method='dense')` Assign Unique Ranks to Tied Values?

Pandas dense ranking: assign unique rankings to tied values ​​

In data analysis, it is often necessary to rank values ​​in a data set based on order. In Pandas, the rank function provides a way to calculate ordinal ranking by default. However, sometimes it is necessary to obtain dense rankings, where tied values ​​receive the same ranking rather than being skipped.

Consider the following Pandas data frame:

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

To generate results equivalent to the DENSE_RANK() function on the Year column, proceed as follows:

  • Use Pandas Series.rank with method='dense':
<code>df['Rank'] = df.Year.rank(method='dense').astype(int)

print(df)</code>

This will generate a new column called Rank where each year is assigned a dense ranking:

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

In this example, the tied values ​​for 2013 receive the same rank of 2, ensuring that no values ​​are skipped or ranked incorrectly. The astype(int) conversion is optional, but its use is recommended to ensure that rankings are stored as integers.

The above is the detailed content of How Does Pandas `rank(method='dense')` Assign Unique Ranks to Tied Values?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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