Employee Data Analysis using Python and Matplotlib
Complete Python Code: Employee Data Analysis using MySQL + Pandas + Matplotlib
Requirements:
pip install mysql-connector-python pandas matplotlib openpyxl
------------------------------------------------------------
1. Import Libraries
------------------------------------------------------------
import [Link]
import pandas as pd
import [Link] as plt
------------------------------------------------------------
2. Connect to MySQL Database
------------------------------------------------------------
conn = [Link](
host="localhost",
user="root",
password="your_password",
database="employee_db"
)
------------------------------------------------------------
3. Load Data from Database
------------------------------------------------------------
query = '''
SELECT e.emp_id, [Link], d.dept_name,
[Link], e.hire_date
FROM employees e
JOIN departments d ON e.dept_id=d.dept_id
JOIN salaries s ON e.emp_id=s.emp_id
'''
df = pd.read_sql(query, conn)
------------------------------------------------------------
4. Employees per Department (Bar Plot)
------------------------------------------------------------
dept_count = df['dept_name'].value_counts()
[Link](dept_count.index, dept_count.values)
[Link]("Employees per Department")
[Link]("Department")
[Link]("Employee Count")
[Link](rotation=45)
[Link]()
------------------------------------------------------------
5. Average Salary per Department
------------------------------------------------------------
dept_salary = [Link]('dept_name')['salary'].mean()
[Link](dept_salary.index, dept_salary.values)
[Link]("Average Salary by Department")
[Link]("Department")
[Link]("Average Salary")
[Link](rotation=45)
[Link]()
------------------------------------------------------------
6. Salary Distribution Histogram
------------------------------------------------------------
[Link](df['salary'], bins=6)
[Link]("Salary Distribution")
[Link]("Salary")
[Link]("Employees")
[Link]()
------------------------------------------------------------
7. Department Salary Share (Pie Chart)
------------------------------------------------------------
dept_total = [Link]('dept_name')['salary'].sum()
[Link](dept_total, labels=dept_total.index, autopct='%1.1f%%')
[Link]("Department Salary Expense Share")
[Link]()
------------------------------------------------------------
8. Hiring Trend Line Plot
------------------------------------------------------------
df['hire_date'] = pd.to_datetime(df['hire_date'])
hire_trend = [Link](df['hire_date'].[Link]).size()
[Link](hire_trend.index, hire_trend.values, marker='o')
[Link]("Hiring Trend by Month")
[Link]("Month")
[Link]("Employees Hired")
[Link]()
------------------------------------------------------------
9. Salary vs Experience Scatter Plot
------------------------------------------------------------
df['experience'] = (
[Link]() - df['hire_date']
).[Link] / 365
[Link](df['experience'], df['salary'])
[Link]("Experience vs Salary")
[Link]("Experience (Years)")
[Link]("Salary")
[Link]()
------------------------------------------------------------
10. Salary Spread by Department (Box Plot)
------------------------------------------------------------
[Link](column='salary', by='dept_name')
[Link]("Salary Distribution by Department")
[Link]("Department")
[Link]("Salary")
[Link](rotation=45)
[Link]()
------------------------------------------------------------
11. Top Paid Employees (Horizontal Bar Plot)
------------------------------------------------------------
top_paid = df.sort_values(by='salary', ascending=False).head(5)
[Link](top_paid['name'], top_paid['salary'])
[Link]("Top Paid Employees")
[Link]("Salary")
[Link]()
------------------------------------------------------------
12. Save Result to Excel
------------------------------------------------------------
df.to_excel("employee_report.xlsx", index=False)
------------------------------------------------------------
End of Program
------------------------------------------------------------