Skip to content

Commit

Permalink
feat: add pandas solutions to lc problems: No.0185,0196 (doocs#1873)
Browse files Browse the repository at this point in the history
  • Loading branch information
nrhitik authored Oct 25, 2023
1 parent e249ecb commit a2eda11
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 0 deletions.
26 changes: 26 additions & 0 deletions solution/0100-0199/0185.Department Top Three Salaries/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,30 @@ FROM
WHERE rk <= 3;
```

### **Pandas**

```python
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"]]

```

<!-- tabs:end -->
26 changes: 26 additions & 0 deletions solution/0100-0199/0185.Department Top Three Salaries/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,30 @@ FROM
WHERE rk <= 3;
```

### **Pandas**

```python
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"]]

```

<!-- tabs:end -->
20 changes: 20 additions & 0 deletions solution/0100-0199/0185.Department Top Three Salaries/Solution.py
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"]]
15 changes: 15 additions & 0 deletions solution/0100-0199/0196.Delete Duplicate Emails/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,19 @@ WHERE
p1.id < p2.id;
```

### **Pandas**

```python
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)

```

<!-- tabs:end -->
15 changes: 15 additions & 0 deletions solution/0100-0199/0196.Delete Duplicate Emails/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,19 @@ WHERE
p1.id < p2.id;
```

### **Pandas**

```python
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)

```

<!-- tabs:end -->
9 changes: 9 additions & 0 deletions solution/0100-0199/0196.Delete Duplicate Emails/Solution.py
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)

0 comments on commit a2eda11

Please sign in to comment.