Home >Database >Mysql Tutorial >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:
<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!