【无标题】

力扣试题`

本文章讲述三道力扣试题题解

181超过经理工资的员工

import pandas as pd

def find_employees(Employee: pd.DataFrame) -> pd.DataFrame:
    Employee_with_manager = Employee.merge(Employee, left_on='managerId', right_on='id', suffixes=('_emp', '_mgr'))

    Employee_with_higher_salary = Employee_with_manager[Employee_with_manager['salary_emp'] > Employee_with_manager['salary_mgr']]

    result = Employee_with_higher_salary[[ 'name_emp']].rename(columns={'name_emp': 'Employee'})
    return result

570至少有五名下属的经理

import pandas as pd

def find_managers(employee: pd.DataFrame) -> pd.DataFrame:
    subordinate_count = employee.groupby('managerId')['id'].count()
    managers_with_5_subordinates = subordinate_count[subordinate_count >= 5].index
    result = employee[employee['id'].isin(managers_with_5_subordinates)]['name']
    result_df = result.to_frame(name='name')
    return result_df

185找出部门工资前三高的员工

import pandas as pd

def top_three_salaries(employee: pd.DataFrame, department: pd.DataFrame) -> pd.DataFrame:
    df=employee.merge(department,left_on='departmentId',right_on='id')
    df['rank']=df.groupby('departmentId')['salary'].rank(method='dense',ascending=False)
    df=df[df['rank']<=3]
  	return
  	pd.DataFrame({'Department':df['name_y'],'Employee':df['name_x'],'Salary':df['salary']})

以上是可通过的代码示例,但仍有优化的进步空间,等待大家去挖掘。

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值