Skip to content

Commit a2eda11

Browse files
authored
feat: add pandas solutions to lc problems: No.0185,0196 (doocs#1873)
1 parent e249ecb commit a2eda11

File tree

6 files changed

+111
-0
lines changed

6 files changed

+111
-0
lines changed

Diff for: solution/0100-0199/0185.Department Top Three Salaries/README.md

+26
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,30 @@ FROM
139139
WHERE rk <= 3;
140140
```
141141

142+
### **Pandas**
143+
144+
```python
145+
import pandas as pd
146+
147+
148+
def top_three_salaries(
149+
employee: pd.DataFrame, department: pd.DataFrame
150+
) -> pd.DataFrame:
151+
salary_cutoff = (
152+
employee.drop_duplicates(["salary", "departmentId"])
153+
.groupby("departmentId")["salary"]
154+
.nlargest(3)
155+
.groupby("departmentId")
156+
.min()
157+
)
158+
employee["Department"] = department.set_index("id")["name"][
159+
employee["departmentId"]
160+
].values
161+
employee["cutoff"] = salary_cutoff[employee["departmentId"]].values
162+
return employee[employee["salary"] >= employee["cutoff"]].rename(
163+
columns={"name": "Employee", "salary": "Salary"}
164+
)[["Department", "Employee", "Salary"]]
165+
166+
```
167+
142168
<!-- tabs:end -->

Diff for: solution/0100-0199/0185.Department Top Three Salaries/README_EN.md

+26
Original file line numberDiff line numberDiff line change
@@ -135,4 +135,30 @@ FROM
135135
WHERE rk <= 3;
136136
```
137137

138+
### **Pandas**
139+
140+
```python
141+
import pandas as pd
142+
143+
144+
def top_three_salaries(
145+
employee: pd.DataFrame, department: pd.DataFrame
146+
) -> pd.DataFrame:
147+
salary_cutoff = (
148+
employee.drop_duplicates(["salary", "departmentId"])
149+
.groupby("departmentId")["salary"]
150+
.nlargest(3)
151+
.groupby("departmentId")
152+
.min()
153+
)
154+
employee["Department"] = department.set_index("id")["name"][
155+
employee["departmentId"]
156+
].values
157+
employee["cutoff"] = salary_cutoff[employee["departmentId"]].values
158+
return employee[employee["salary"] >= employee["cutoff"]].rename(
159+
columns={"name": "Employee", "salary": "Salary"}
160+
)[["Department", "Employee", "Salary"]]
161+
162+
```
163+
138164
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import pandas as pd
2+
3+
4+
def top_three_salaries(
5+
employee: pd.DataFrame, department: pd.DataFrame
6+
) -> pd.DataFrame:
7+
salary_cutoff = (
8+
employee.drop_duplicates(["salary", "departmentId"])
9+
.groupby("departmentId")["salary"]
10+
.nlargest(3)
11+
.groupby("departmentId")
12+
.min()
13+
)
14+
employee["Department"] = department.set_index("id")["name"][
15+
employee["departmentId"]
16+
].values
17+
employee["cutoff"] = salary_cutoff[employee["departmentId"]].values
18+
return employee[employee["salary"] >= employee["cutoff"]].rename(
19+
columns={"name": "Employee", "salary": "Salary"}
20+
)[["Department", "Employee", "Salary"]]

Diff for: solution/0100-0199/0196.Delete Duplicate Emails/README.md

+15
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,19 @@ WHERE
9797
p1.id < p2.id;
9898
```
9999

100+
### **Pandas**
101+
102+
```python
103+
import pandas as pd
104+
105+
106+
# Modify Person in place
107+
def delete_duplicate_emails(person: pd.DataFrame) -> None:
108+
# Sort the rows based on id (Ascending order)
109+
person.sort_values(by="id", ascending=True, inplace=True)
110+
# Drop the duplicates based on email.
111+
person.drop_duplicates(subset="email", keep="first", inplace=True)
112+
113+
```
114+
100115
<!-- tabs:end -->

Diff for: solution/0100-0199/0196.Delete Duplicate Emails/README_EN.md

+15
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,19 @@ WHERE
9393
p1.id < p2.id;
9494
```
9595

96+
### **Pandas**
97+
98+
```python
99+
import pandas as pd
100+
101+
102+
# Modify Person in place
103+
def delete_duplicate_emails(person: pd.DataFrame) -> None:
104+
# Sort the rows based on id (Ascending order)
105+
person.sort_values(by="id", ascending=True, inplace=True)
106+
# Drop the duplicates based on email.
107+
person.drop_duplicates(subset="email", keep="first", inplace=True)
108+
109+
```
110+
96111
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import pandas as pd
2+
3+
4+
# Modify Person in place
5+
def delete_duplicate_emails(person: pd.DataFrame) -> None:
6+
# Sort the rows based on id (Ascending order)
7+
person.sort_values(by="id", ascending=True, inplace=True)
8+
# Drop the duplicates based on email.
9+
person.drop_duplicates(subset="email", keep="first", inplace=True)

0 commit comments

Comments
 (0)