Skip to content

Commit e24cd4d

Browse files
authored
feat: add pandas solution to lc problem: No.0570 (#1892)
1 parent 00ce7cb commit e24cd4d

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

solution/0500-0599/0570.Managers with at Least 5 Direct Reports/README.md

+27
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,31 @@ FROM
8181
USING (id);
8282
```
8383

84+
### **Pandas**
85+
86+
```python
87+
import pandas as pd
88+
89+
90+
def find_managers(employee: pd.DataFrame) -> pd.DataFrame:
91+
# Group the employees by managerId and count the number of direct reports
92+
manager_report_count = (
93+
employee.groupby("managerId").size().reset_index(name="directReports")
94+
)
95+
96+
# Filter managers with at least five direct reports
97+
result = manager_report_count[manager_report_count["directReports"] >= 5]
98+
99+
# Merge with the Employee table to get the names of these managers
100+
result = result.merge(
101+
employee[["id", "name"]], left_on="managerId", right_on="id", how="inner"
102+
)
103+
104+
# Select only the 'name' column and drop the 'id' and 'directReports' columns
105+
result = result[["name"]]
106+
107+
return result
108+
109+
```
110+
84111
<!-- tabs:end -->

solution/0500-0599/0570.Managers with at Least 5 Direct Reports/README_EN.md

+27
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,31 @@ FROM
7777
USING (id);
7878
```
7979

80+
### **Pandas**
81+
82+
```python
83+
import pandas as pd
84+
85+
86+
def find_managers(employee: pd.DataFrame) -> pd.DataFrame:
87+
# Group the employees by managerId and count the number of direct reports
88+
manager_report_count = (
89+
employee.groupby("managerId").size().reset_index(name="directReports")
90+
)
91+
92+
# Filter managers with at least five direct reports
93+
result = manager_report_count[manager_report_count["directReports"] >= 5]
94+
95+
# Merge with the Employee table to get the names of these managers
96+
result = result.merge(
97+
employee[["id", "name"]], left_on="managerId", right_on="id", how="inner"
98+
)
99+
100+
# Select only the 'name' column and drop the 'id' and 'directReports' columns
101+
result = result[["name"]]
102+
103+
return result
104+
105+
```
106+
80107
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import pandas as pd
2+
3+
4+
def find_managers(employee: pd.DataFrame) -> pd.DataFrame:
5+
# Group the employees by managerId and count the number of direct reports
6+
manager_report_count = (
7+
employee.groupby("managerId").size().reset_index(name="directReports")
8+
)
9+
10+
# Filter managers with at least five direct reports
11+
result = manager_report_count[manager_report_count["directReports"] >= 5]
12+
13+
# Merge with the Employee table to get the names of these managers
14+
result = result.merge(
15+
employee[["id", "name"]], left_on="managerId", right_on="id", how="inner"
16+
)
17+
18+
# Select only the 'name' column and drop the 'id' and 'directReports' columns
19+
result = result[["name"]]
20+
21+
return result

0 commit comments

Comments
 (0)