Python Central HubSign in

Data Analytics with Python / Phase 10: Data Analytics Projects

HR Analytics Dashboard

Analyze HR data - attrition rates, segment comparisons, and dashboard-ready charts.

Goal

From HR data, answer:

  • Attrition rate
  • Attrition by department/job role
  • Salary and tenure differences

Analysis pipeline

diagram HR analytics pipeline mermaid
From raw employee records to attrition insights by segment.

Step 1: Load

Python
import pandas as pd
 
df = pd.read_csv("data/hr.csv")
print(df.head())

Step 2: Attrition rate

Python
rate = df["attrition"].mean()  # assuming attrition is 0/1
print("Attrition rate:", rate)

Step 3: Bar plots by segment

Python
import seaborn as sns
import matplotlib.pyplot as plt
 
plt.figure(figsize=(10, 4))
sns.barplot(data=df, x="department", y="attrition")
plt.title("Attrition rate by department")
plt.xticks(rotation=20)
plt.tight_layout()
plt.show()

Step 4: Box plots for numeric features

Python
import seaborn as sns
import matplotlib.pyplot as plt
 
plt.figure(figsize=(7, 4))
sns.boxplot(data=df, x="attrition", y="salary")
plt.title("Salary vs attrition")
plt.tight_layout()
plt.show()

Visualize it

sketch Attrition rate by department p5.js
Departments with taller amber bars need attention first.

Deliverable

A dashboard should include:

  • Attrition KPI
  • Top segments with high attrition
  • Suggested interventions (training, compensation, career path)

Why attrition rate is a mean

Attrition is a yes-or-no outcome for each employee. If you store it as 0 for "stayed" and 1 for "left", then the mean of the column is exactly the share of people who left, which is the attrition rate. A mean of 0.16 means 16 percent left. This trick is used everywhere in analytics: a 0/1 column makes rates, conversion and churn one mean() away, and groupby turns it into a rate per segment.

Real HR files often store the column as the text "Yes" and "No". Convert it first, or the mean will fail:

Python
import pandas as pd
 
df = pd.DataFrame({
    "department": ["Sales", "Sales", "IT", "IT", "HR", "HR"],
    "attrition": ["Yes", "No", "No", "No", "Yes", "Yes"],
    "salary": [50, 60, 80, 90, 55, 52],
})
 
df["attrition"] = (df["attrition"] == "Yes").astype(int)
 
print(df["attrition"].mean())                        # 0.5
print(df.groupby("department")["attrition"].mean())
# HR       1.0
# IT       0.0
# Sales    0.5

Reading the segment comparison

The overall rate hides differences between groups. A company at 16 percent overall may have one department at 4 percent and another at 30 percent, and only the second needs action. Group by each dimension you care about (department, job role, tenure band) and sort from highest to lowest so the segments needing attention appear first.

Be careful with small groups. A department with three employees can show 100 percent attrition after one person leaves. Always print the group size next to the rate:

Python
summary = df.groupby("department")["attrition"].agg(rate="mean", people="count")
print(summary.sort_values("rate", ascending=False))

Interpreting the charts

sns.barplot with a 0/1 target draws the mean per group, which is the rate, and adds a confidence interval line. A long interval signals a small or noisy group. The box plot of salary by attrition compares the middle 50 percent of pay for people who stayed and left. If the "left" box sits lower, pay may be a factor, but that is a hint and not proof, since other variables such as role and tenure can explain the gap.

Common mistakes

  • Averaging a text column. Convert "Yes/No" to 1/0 first.
  • Ignoring group size, and reporting big percentages from tiny groups.
  • Claiming a cause. Lower pay among leavers does not prove pay made them leave.
  • Using different definitions of attrition in different charts, such as voluntary in one and all exits in another.
  • Charts without labels, which make a dashboard unreadable to managers.

Turning it into a dashboard

Start with the headline KPI, then show the two or three segments with the highest rate, then the drivers (salary, tenure, overtime), and finish with recommendations. Every chart should answer one question that an HR manager would actually ask.

Check your understanding

quizCheck yourself
  1. Why is the attrition rate a mean?

    Show answer

    A โ€” With 0 for stayed and 1 for left, the mean is the share who left โ€” The mean of a 0/1 column is a rate.

  2. The raw column holds the text Yes and No. What must you do before averaging?

    Show answer

    A โ€” Convert it to 1 and 0, for example (df["attrition"] == "Yes").astype(int) โ€” The mean of text fails, so convert first.

  3. Why print the group size next to each attrition rate?

    Show answer

    A โ€” A tiny group can show 100 percent attrition after one person leaves โ€” Rates from tiny groups are unreliable, so show people count alongside rate.

  4. Lower pay among leavers is observed. What is the correct conclusion?

    Show answer

    A โ€” It does not prove pay caused them to leave โ€” The lesson warns against claiming a cause from an association.

๐Ÿงช Try It Yourself

Exercise 1 โ€“ Overall attrition rate

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 2 โ€“ Attrition by department

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 3 โ€“ Two-way attrition pivot table

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Was this page helpful?

Let us know how we did