From 7336e9ffee12af30a04b91f9aad92fd0b8a02586 Mon Sep 17 00:00:00 2001 From: NehaDhaliwalNehaDhaliwal Date: Sat, 8 Mar 2025 08:28:39 -0600 Subject: [PATCH] Done Pandas5 --- Problem 1.py | 16 ++++++++++++++++ Problem 2.py | 12 ++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 Problem 1.py create mode 100644 Problem 2.py diff --git a/Problem 1.py b/Problem 1.py new file mode 100644 index 0000000..806b009 --- /dev/null +++ b/Problem 1.py @@ -0,0 +1,16 @@ +# Department Highest Salary + +import pandas as pd + +def department_highest_salary(employee: pd.DataFrame, department: pd.DataFrame) -> pd.DataFrame: + df = employee[employee['salary'] == employee.groupby('departmentId')['salary'].transform('max')] + df = df.merge(department, right_on = 'id', left_on = 'departmentId') + df = df[['name_y', 'name_x', 'salary']].rename(columns = {'name_y':'department', 'name_x':'employee'}) + print(df) + return df + +data = [[1, 'Joe', 70000, 1], [2, 'Jim', 90000, 1], [3, 'Henry', 80000, 2], [4, 'Sam', 60000, 2], [5, 'Max', 90000, 1]] +employee = pd.DataFrame(data, columns=['id', 'name', 'salary', 'departmentId']).astype({'id':'Int64', 'name':'object', 'salary':'Int64', 'departmentId':'Int64'}) +data = [[1, 'IT'], [2, 'Sales']] +department = pd.DataFrame(data, columns=['id', 'name']).astype({'id':'Int64', 'name':'object'}) +department_highest_salary(employee, department) \ No newline at end of file diff --git a/Problem 2.py b/Problem 2.py new file mode 100644 index 0000000..a818c87 --- /dev/null +++ b/Problem 2.py @@ -0,0 +1,12 @@ +# Rank Scores + +import pandas as pd + +def order_scores(scores: pd.DataFrame) -> pd.DataFrame: + scores['rank'] = scores['score'].rank(method='dense', ascending=False) + print(scores[['score', 'rank']].sort_values('score', ascending=False)) + return scores[['score', 'rank']].sort_values('score', ascending=False) + +data = [[1, 3.5], [2, 3.65], [3, 4.0], [4, 3.85], [5, 4.0], [6, 3.65]] +scores = pd.DataFrame(data, columns=['id', 'score']).astype({'id':'Int64', 'score':'Float64'}) +order_scores(scores) \ No newline at end of file