-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path570-ManagersWithAtLeast5DirectReports.py
More file actions
70 lines (63 loc) · 2.65 KB
/
570-ManagersWithAtLeast5DirectReports.py
File metadata and controls
70 lines (63 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# 570. Managers with at Least 5 Direct Reports
# Table: Employee
# +-------------+---------+
# | Column Name | Type |
# +-------------+---------+
# | id | int |
# | name | varchar |
# | department | varchar |
# | managerId | int |
# +-------------+---------+
# id is the primary key column for this table.
# Each row of this table indicates the name of an employee, their department, and the id of their manager.
# If managerId is null, then the employee does not have a manager.
# No employee will be the manager of themself.
#
# Write an SQL query to report the managers with at least five direct reports.
# Return the result table in any order.
# The query result format is in the following example.
# Example 1:
# Input:
# Employee table:
# +-----+-------+------------+-----------+
# | id | name | department | managerId |
# +-----+-------+------------+-----------+
# | 101 | John | A | None |
# | 102 | Dan | A | 101 |
# | 103 | James | A | 101 |
# | 104 | Amy | A | 101 |
# | 105 | Anne | A | 101 |
# | 106 | Ron | B | 101 |
# +-----+-------+------------+-----------+
# Output:
# +------+
# | name |
# +------+
# | John |
# +------+
import pandas as pd
def find_managers(employee: pd.DataFrame) -> pd.DataFrame:
#manager = employee.groupby('managerId').count().reset_index(name = "count")
manager = employee.groupby('managerId').size().reset_index(name="count")
filter = manager["count"] >= 5
df = manager[filter][["managerId"]]
print(df)
print(df.index)
return employee[employee["id"].isin(df.index)][["name"]]
# merge
def find_managers1(employee: pd.DataFrame) -> pd.DataFrame:
manager = employee.groupby('managerId').size().reset_index(name='count')
manager = manager[manager['count'] >= 5 ]
df = manager.merge(employee, left_on='managerId', right_on='id', how='inner')
return df[['name']]
def find_managers2(employee: pd.DataFrame) -> pd.DataFrame:
employee['managerId'] = employee['managerId'].fillna(employee['id'])
cnt = employee.groupby('managerId').size()
inds = cnt[cnt >= 5].index
return employee[employee['id'].isin(inds)][['name']]
if __name__ == "__main__":
data = [[101, 'John', 'A', None], [102, 'Dan', 'A', 101], [103, 'James', 'A', 101], [104, 'Amy', 'A', 101], [105, 'Anne', 'A', 101], [106, 'Ron', 'B', 101]]
employee = pd.DataFrame(data, columns=['id', 'name', 'department', 'managerId']).astype({'id':'Int64', 'name':'object', 'department':'object', 'managerId':'Int64'})
print(find_managers(employee))
print(find_managers1(employee))
print(find_managers2(employee))