From da6d02e6ef970bcccf22a1f9d5e274b93ec0465d Mon Sep 17 00:00:00 2001 From: Vichitravir Dwivedi <93407819+vichitravird@users.noreply.github.com> Date: Sun, 6 Oct 2024 20:53:49 -0500 Subject: [PATCH] Update README.md --- README.md | 38 +++++++++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index ded7a6c..4640cbf 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,39 @@ -# Pandas5 +## Pandas5 -1 Problem 1 : Department Highest Salary (https://leetcode.com/problems/department-highest-salary/ ) +### 1 Problem 1 : Department Highest Salary (https://leetcode.com/problems/department-highest-salary/ ) +
+import pandas as pd -2 Problem 2 : Rank Scores ( https://leetcode.com/problems/rank-scores/ ) +def department_highest_salary(employee: pd.DataFrame, department: pd.DataFrame) -> pd.DataFrame: + if employee.empty or department.empty: + return pd.DataFrame(columns=['Department','Employee', 'Salary']) + + # Merge the employee and department DataFrames on 'departmentId' and 'id' columns + merged_df = employee.merge(department, left_on='departmentId', right_on='id', suffixes=('_employee', '_department')) + + # Use groupby to group data by 'departmentId' and apply a lambda function to get employees with highest salary in each group + highest_salary_df = merged_df.groupby('departmentId').apply(lambda x: x[x['salary'] == x['salary'].max()]) + + # Drop the duplicate 'departmentId' column and reset the index + highest_salary_df = highest_salary_df.reset_index(drop=True) + + # Select the required columns and return the result + result_df = highest_salary_df[['name_department', 'name_employee', 'salary']] + + # Rename the columns as specified + result_df.columns = ['Department','Employee', 'Salary'] + + return result_df +### 2 Problem 2 : Rank Scores ( https://leetcode.com/problems/rank-scores/ ) +
+ +def order_scores(scores: pd.DataFrame) -> pd.DataFrame: + # Use the rank method to assign ranks to the scores in descending order with no gaps + scores['rank'] = scores['score'].rank(method='dense', ascending=False) + + # Drop id column & Sort the DataFrame by score in descending order + result_df = scores.drop('id',axis=1).sort_values(by='score', ascending=False) + + return result_df