Skip to content

Conversation

@aryaman0406
Copy link
Owner

The provided Python code uses the pandas library and a manual counting and sorting approach to rank scores in a DataFrame. This solution addresses LeetCode Problem 178: Rank Scores (Pandas/SQL). However, the manual approach is inefficient and not idiomatic for Pandas.

I will complete the PR template using an idiomatic Pandas approach, which is the preferred and most efficient method for this type of problem.

Note: Since the problem is usually solved in Pandas using the built-in ranking function, I'll provide the optimal Pandas solution alongside the structured explanation.

PR Title Format: 178.Rank Scores.py

💡 Intuition
The goal is to assign a rank to each score in a DataFrame such that higher scores get a higher (smaller numerical) rank. Crucially, scores that are equal must receive the same rank (a "dense" ranking), and the next distinct score must receive the rank immediately following the common rank. This requirement perfectly matches the behavior of the built-in Dense Rank function, available in both SQL and Pandas. Using DataFrame.rank(method='dense') is the most direct and efficient approach.

✍️ Approach
The solution uses the pandas.DataFrame.rank() method, which is specifically designed for ranking tasks.

Sorting: Although the rank function can handle unsorted data, the problem requires the final output to be sorted by score in descending order. We apply sort_values(by='score', ascending=False) to the input DataFrame first.

Ranking: The rank() method is called on the score column.

method='dense': This is the key parameter. It ensures that equal scores receive the same rank and that the rank sequence is gap-free (e.g., if two scores get rank 1, the next score gets rank 2, not 3).

ascending=False: This ensures that higher scores receive a lower rank number (e.g., the highest score gets rank 1).

Formatting: The calculated ranks (which are floats by default) are cast to integers. The final DataFrame is returned with only the score and the new rank columns.

Code Solution (Python/Pandas)
Python

import pandas as pd
import numpy as np

def order_scores(scores: pd.DataFrame) -> pd.DataFrame:
# 1. Sort the DataFrame by score in descending order
scores = scores.sort_values(by='score', ascending=False)

# 2. Apply dense ranking to the 'score' column
#    method='dense': assigns same rank to equal values, no gaps in ranks.
#    ascending=False: higher scores get a lower rank number (Rank 1).
scores['rank'] = scores['score'].rank(method='dense', ascending=False).astype(int)

# 3. Return the result with only the requested columns
return scores[['score', 'rank']]

🔗 Related Issues
By submitting this PR, I confirm that:

[x] This is my original work not totally AI generated

[x] I have tested the solution thoroughly on leetcode

[x] I have maintained proper PR description format

[x] This is a meaningful contribution, not spam

Implement a function to rank scores in a DataFrame.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants