gpt4 book ai didi

python - 我如何根据部门汇总员工并使用 groupby pandas 显示每个部门的平均工资?

转载 作者:行者123 更新时间:2023-12-03 13:56:19 24 4
gpt4 key购买 nike

这是我的代码,其中包含我想使用 执行任务的数据pandas.DataFrame.groupby

import pandas as pd
data = {'employees_no': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13],
'employees_name': ['Jugal Sompura', 'Maya Rajput', 'Chaitya Panchal', 'Sweta Rampariya', 'Prakshal Patel', 'Dhruv Panchal', 'Prachi Desai', 'Krunal Gosai', 'Hemil Soni', 'Gopal Pithadia', 'Jatin Shah', 'Raj Patel', 'Shreya Desai'],
'department_name': ['HR', 'Administrative Assistant', 'Production', 'Accountant', 'Production', 'Engineer', 'Finance', 'Engineer', 'Quality Assurance', 'Engineer', 'Engineer', 'Customer Service', 'CEO'],
'salary': [130000.0, 65000.0, 45000.0, 65000.0, 47000.0, 40000.0, 90000.0, 45000.0, 35000.0, 45000.0, 30000.0, 40000.0, 250000.0]
}
df = pd.DataFrame (data, columns = ['employees_no', 'employees_name', 'department_name', 'salary'])
print(df)
---------------------------------------------------------------------
employees_no employees_name department_name salary
0 1 Jugal Sompura HR 130000.0
1 2 Maya Rajput Administrative Assistant 65000.0
2 3 Chaitya Panchal Production 45000.0
3 4 Sweta Rampariya Accountant 65000.0
4 5 Prakshal Patel Production 47000.0
5 6 Dhruv Panchal Engineer 40000.0
6 7 Prachi Desai Finance 90000.0
7 8 Krunal Gosai Engineer 45000.0
8 9 Hemil Soni Quality Assurance 35000.0
9 10 Gopal Pithadia Engineer 45000.0
10 11 Jatin Shah Engineer 30000.0
11 12 Raj Patel Customer Service 40000.0
12 13 Shreya Desai CEO 250000.0
---------------------------------------------------------------------
我试过了,只能得到这个输出。
print(df.groupby('department_name').agg({'salary':'mean'}))
---------------------------------------------------------------------
department_name salary
Accountant 65000.0
Administrative Assistant 65000.0
CEO 250000.0
Customer Service 40000.0
Engineer 40000.0
Finance 90000.0
HR 130000.0
Production 46000.0
Quality Assurance 35000.0
---------------------------------------------------------------------
我无法获得这样的输出...
department_name          employees_name     avg_salary        
Accountant Sweta Rampariya 65000.0
Administrative Assistant Maya Rajput 65000.0
CEO Shreya Desai 250000.0
Customer Service Raj Patel 40000.0
Engineer Dhruv Panchal 40000.0
Gopal Pithadia
Krunal Gosai
Jatin Shah
Finance Prachi Desai 90000.0
HR Jugal Sompura 130000.0
Production Chaitya Panchal 46000.0
Prakshal Patel
Quality Assurance Hemil Soni 35000.0
你能帮我解决这个问题吗?

最佳答案

您需要 pandas.DataFrame.groupby.transform :

df["avg_salary"] = df.groupby("department_name")["salary"].transform("mean")
new_df = df.set_index(["department_name", "employees_name"]).sort_index()
print(new_df["avg_salary"])
输出:
department_name           employees_name 
Accountant Sweta Rampariya 65000.0
Administrative Assistant Maya Rajput 65000.0
CEO Shreya Desai 250000.0
Customer Service Raj Patel 40000.0
Engineer Dhruv Panchal 40000.0
Gopal Pithadia 40000.0
Jatin Shah 40000.0
Krunal Gosai 40000.0
Finance Prachi Desai 90000.0
HR Jugal Sompura 130000.0
Production Chaitya Panchal 46000.0
Prakshal Patel 46000.0
Quality Assurance Hemil Soni 35000.0
Name: avg_salary, dtype: float64

关于python - 我如何根据部门汇总员工并使用 groupby pandas 显示每个部门的平均工资?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64113002/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com