-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path6.py
More file actions
35 lines (26 loc) · 1.08 KB
/
Copy path6.py
File metadata and controls
35 lines (26 loc) · 1.08 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
import pandas as pd
def problem_statement_01(data: dict):
"""
Top 5 salary by department
"""
df = pd.DataFrame(data)
#solution 1
result = df.groupby("Department")["Salary"].nlargest(5).reset_index()
print('Solution 1\n', result, '\n')
# solution 2
df['rank'] = df.groupby('Department')['Salary'].rank(method='first', ascending=False)
result = df[df['rank'] <= 5].sort_values(['Department', 'rank'])
print('Solution 2\n', result, '\n')
#solution 3
result = df.sort_values(
['Department', 'Salary'], ascending=[True, False]
).groupby('Department').head(5)
print('Solution 3\n', result, '\n')
return result
if __name__ == "__main__":
data = {
"emp_name": ["Alice", "Bob", "Charlie", "David", "Eve", "Frank", "Grace", "Heidi", "Ivan", "Judy", "Ken", "Leo"],
"Department": ["HR", "HR", "IT", "IT", "Finance", "Finance", "HR", "IT", "Finance", "HR", "IT", "Finance"],
"Salary": [70000, 75000, 90000, 85000, 80000, 95000, 72000, 88000, 82000, 78000, 92000, 98000]
}
problem_statement_01(data)