Skip to content

Commit 2e5040d

Browse files
authored
feat: add pandas solution to lc problem: No.0178 (doocs#1861)
1 parent a1d1d6b commit 2e5040d

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

solution/0100-0199/0178.Rank Scores/README.md

+15
Original file line numberDiff line numberDiff line change
@@ -122,4 +122,19 @@ FROM
122122
) s;
123123
```
124124

125+
```python
126+
import pandas as pd
127+
128+
129+
def order_scores(scores: pd.DataFrame) -> pd.DataFrame:
130+
# Use the rank method to assign ranks to the scores in descending order with no gaps
131+
scores["rank"] = scores["score"].rank(method="dense", ascending=False)
132+
133+
# Drop id column & Sort the DataFrame by score in descending order
134+
result_df = scores.drop("id", axis=1).sort_values(by="score", ascending=False)
135+
136+
return result_df
137+
138+
```
139+
125140
<!-- tabs:end -->

solution/0100-0199/0178.Rank Scores/README_EN.md

+15
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,19 @@ FROM
113113
) s;
114114
```
115115

116+
```python
117+
import pandas as pd
118+
119+
120+
def order_scores(scores: pd.DataFrame) -> pd.DataFrame:
121+
# Use the rank method to assign ranks to the scores in descending order with no gaps
122+
scores["rank"] = scores["score"].rank(method="dense", ascending=False)
123+
124+
# Drop id column & Sort the DataFrame by score in descending order
125+
result_df = scores.drop("id", axis=1).sort_values(by="score", ascending=False)
126+
127+
return result_df
128+
129+
```
130+
116131
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import pandas as pd
2+
3+
4+
def order_scores(scores: pd.DataFrame) -> pd.DataFrame:
5+
# Use the rank method to assign ranks to the scores in descending order with no gaps
6+
scores["rank"] = scores["score"].rank(method="dense", ascending=False)
7+
8+
# Drop id column & Sort the DataFrame by score in descending order
9+
result_df = scores.drop("id", axis=1).sort_values(by="score", ascending=False)
10+
11+
return result_df

0 commit comments

Comments
 (0)