Skip to content

feat: add pandas solution to lc problem: No.0570. #1892

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,31 @@ FROM
USING (id);
```

### **Pandas**

```python
import pandas as pd


def find_managers(employee: pd.DataFrame) -> pd.DataFrame:
# Group the employees by managerId and count the number of direct reports
manager_report_count = (
employee.groupby("managerId").size().reset_index(name="directReports")
)

# Filter managers with at least five direct reports
result = manager_report_count[manager_report_count["directReports"] >= 5]

# Merge with the Employee table to get the names of these managers
result = result.merge(
employee[["id", "name"]], left_on="managerId", right_on="id", how="inner"
)

# Select only the 'name' column and drop the 'id' and 'directReports' columns
result = result[["name"]]

return result

```

<!-- tabs:end -->
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,31 @@ FROM
USING (id);
```

### **Pandas**

```python
import pandas as pd


def find_managers(employee: pd.DataFrame) -> pd.DataFrame:
# Group the employees by managerId and count the number of direct reports
manager_report_count = (
employee.groupby("managerId").size().reset_index(name="directReports")
)

# Filter managers with at least five direct reports
result = manager_report_count[manager_report_count["directReports"] >= 5]

# Merge with the Employee table to get the names of these managers
result = result.merge(
employee[["id", "name"]], left_on="managerId", right_on="id", how="inner"
)

# Select only the 'name' column and drop the 'id' and 'directReports' columns
result = result[["name"]]

return result

```

<!-- tabs:end -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import pandas as pd


def find_managers(employee: pd.DataFrame) -> pd.DataFrame:
# Group the employees by managerId and count the number of direct reports
manager_report_count = (
employee.groupby("managerId").size().reset_index(name="directReports")
)

# Filter managers with at least five direct reports
result = manager_report_count[manager_report_count["directReports"] >= 5]

# Merge with the Employee table to get the names of these managers
result = result.merge(
employee[["id", "name"]], left_on="managerId", right_on="id", how="inner"
)

# Select only the 'name' column and drop the 'id' and 'directReports' columns
result = result[["name"]]

return result