forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add pandas solutions to lc problems: No.0185,0196 (doocs#1873)
- Loading branch information
Showing
6 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
solution/0100-0199/0185.Department Top Three Salaries/Solution.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import pandas as pd | ||
|
||
|
||
def top_three_salaries( | ||
employee: pd.DataFrame, department: pd.DataFrame | ||
) -> pd.DataFrame: | ||
salary_cutoff = ( | ||
employee.drop_duplicates(["salary", "departmentId"]) | ||
.groupby("departmentId")["salary"] | ||
.nlargest(3) | ||
.groupby("departmentId") | ||
.min() | ||
) | ||
employee["Department"] = department.set_index("id")["name"][ | ||
employee["departmentId"] | ||
].values | ||
employee["cutoff"] = salary_cutoff[employee["departmentId"]].values | ||
return employee[employee["salary"] >= employee["cutoff"]].rename( | ||
columns={"name": "Employee", "salary": "Salary"} | ||
)[["Department", "Employee", "Salary"]] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import pandas as pd | ||
|
||
|
||
# Modify Person in place | ||
def delete_duplicate_emails(person: pd.DataFrame) -> None: | ||
# Sort the rows based on id (Ascending order) | ||
person.sort_values(by="id", ascending=True, inplace=True) | ||
# Drop the duplicates based on email. | ||
person.drop_duplicates(subset="email", keep="first", inplace=True) |