diff --git a/solution/0500-0599/0570.Managers with at Least 5 Direct Reports/README.md b/solution/0500-0599/0570.Managers with at Least 5 Direct Reports/README.md index 8b65aaa1755de..9e124c0fa471d 100644 --- a/solution/0500-0599/0570.Managers with at Least 5 Direct Reports/README.md +++ b/solution/0500-0599/0570.Managers with at Least 5 Direct Reports/README.md @@ -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 + +``` + diff --git a/solution/0500-0599/0570.Managers with at Least 5 Direct Reports/README_EN.md b/solution/0500-0599/0570.Managers with at Least 5 Direct Reports/README_EN.md index cd94ee792f168..d04e4aec04ae0 100644 --- a/solution/0500-0599/0570.Managers with at Least 5 Direct Reports/README_EN.md +++ b/solution/0500-0599/0570.Managers with at Least 5 Direct Reports/README_EN.md @@ -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 + +``` + diff --git a/solution/0500-0599/0570.Managers with at Least 5 Direct Reports/Solution.py b/solution/0500-0599/0570.Managers with at Least 5 Direct Reports/Solution.py new file mode 100644 index 0000000000000..e21461b60f43a --- /dev/null +++ b/solution/0500-0599/0570.Managers with at Least 5 Direct Reports/Solution.py @@ -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