Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 35 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -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/ )
<br>
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/ )
<br>

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