How I can aggregate employee based on their department and show average salary in each department using groupby pandas?
You need 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"])
Output:
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
Extending what @Chris did and adding the part of remove average salary values if department_name is same.
Here's the full code:
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)
df['avg_sal'] = df.groupby('department_name')['salary'].transform('mean')
new_df = df.set_index(["department_name", "employees_name"]).sort_index()
new_df.loc[new_df.index.get_level_values(0).duplicated()==True,'avg_sal']=''
print (new_df['avg_sal'])
This will print as follows:
department_name employees_name
Accountant Sweta Rampariya 65000
Administrative Assistant Maya Rajput 65000
CEO Shreya Desai 250000
Customer Service Raj Patel 40000
Engineer Dhruv Panchal 40000
Gopal Pithadia
Jatin Shah
Krunal Gosai
Finance Prachi Desai 90000
HR Jugal Sompura 130000
Production Chaitya Panchal 46000
Prakshal Patel
Quality Assurance Hemil Soni 35000