Payroll and Headcount Analysis in Python — 30 Worked Answers (pandas)
Payroll questions sound simple until you have to answer them precisely. How many people work here — on which date, and does someone who left on the 30th count? What is turnover — leavers over the headcount at the start, the end, or the average of twelve month-ends? This notebook settles each definition once, in code, then answers thirty graded questions on a real payroll of 981 people and 23,072 payslips, ending with the end-of-service liability and a payslip that should never have been paid.
By findi360 · Updated November 2026
- Format
- Jupyter notebook (.ipynb), outputs saved
- Data
- DATA DARE DD-HRM-001 · 981 employees · 23,072 payslips · 2023 to 2026
- Covers
- 30 questions, easy to very hard
- Needs
- Python 3.9+, pandas, openpyxl
The rule that decides ten of the answers
Someone is on the payroll at a date when they were hired on or before it and either have no last working day or a later one. Written as a single function at the top of the notebook, that one rule answers every headcount question, the nationality and gender splits, the turnover calculation and the end-of-service liability. Written loosely — or differently in different cells — it quietly moves ten answers. This is the main thing the file is teaching.
Turnover, and why the denominator matters
Leavers in the year is the easy half. The headcount to divide by is where reports disagree with each other. A business that opened six stores during the year has a very different headcount in January and December, so dividing by either one flatters or punishes the figure. This notebook uses the average of the twelve month-end headcounts, which is why the function above is called twelve times in a single line.
Overtime, cost per head and the opening of a new store
Overtime is read as a share of regular pay rather than in absolute terms, because a big store naturally pays more of it — and the final settlements of leavers are taken out first, since gratuity and leave encashment are not pay for work done that month. Cost per head compares a December payroll against the headcount on the last day of that December. One question looks only at the six stores that opened late in the year, in their very first payroll month, where a new team learning the job shows up as overtime.
What the company owes its people
The last question is the end-of-service benefit: twenty-one days of pay for each of the first five years of service, thirty days a year after that, nothing below a year, and never more than two years' salary. Working it out means taking each person still employed at the year end, finding their December basic salary, measuring their service in years, and applying the formula with its floor and its cap. It is the same calculation an actuary starts from.
Python or Excel?
A payroll register of 23,000 rows is already uncomfortable in a spreadsheet, and the awkward part is not the size — it is that every headcount question needs the same rule applied at a different date. In Python the rule is a function you call with a date. In Excel it is a formula copied across twelve columns, each one a chance to get a boundary wrong.
Inside the notebook
- Libraries — A cell that tells you which libraries are missing before anything else runs.
- Load the sheets — The people, their payslips, the leave taken, the cost centres and the calendar.
- First look — Column types, who is still employed, and what kinds of cost centre exist.
- Fix the data — The pay year, and each payslip joined to the employee behind it.
- The working tables — The headcount rule as a function, payroll by year, the overtime picture by store, and leave taken.
- The answers, Q01 to Q30 — One cell per question, each ending with the answer, and a final list of all thirty.
Excel + Python — the Power of Plus
Excel does this model well when all five of these are true. When two or more are not, you add Python — not instead of Excel, beside it. That is the Power of Plus.
- One-off — you build it once, not again every month.
- Fixed structure — the same accounts, entities and layout each time.
- Small enough to check by eye — thousands of rows, not millions.
- One source — one ledger or one plan, not data scattered across systems and files.
- No live link needed — nobody expects it to update from the ledger by itself.
What next month's people report really costs in Excel
- Paste the new payroll register in and extend every formula to the new rows.
- Rebuild the headcount at twelve month-ends, one column each, each with its own date boundary to get wrong.
- Re-do the joiner and leaver counts, and reconcile them to the headcount movement.
- Re-check the end-of-service provision line by line when salaries change.
The same work, once it repeats
In Python the headcount rule is written once and called with a date; the whole report rebuilds from next month's export without touching a formula.
Python still needs your judgement where the data is new — but everything after that repeats by itself.
Learn it on findi360: Python for Finance — Advanced Analytics & Reporting
Notify me when it opensWant to see what scattered data looks like? Three ERPs, three currencies and one consolidated group: DD-CON-004 — Diversified Group Close: Acquisition, Goodwill & Multi-ERP Consolidation
Who this model is for
- HR and finance teams reporting headcount, turnover and people cost every month
- Accountants who have to prove the payroll journal and the end-of-service provision
- Anyone learning Python who wants a worked example on data they already understand
Frequently asked questions
Do I need to know Python already?
No. Every block is introduced in plain words first, and the comments explain why a line is there. The payroll ideas will be familiar; the Python is deliberately simple.
How is turnover calculated here?
Leavers during the year divided by the average of the twelve month-end headcounts, as a percentage. The notebook shows the average it used, so you can compare it with whatever definition your own reports use.
Can I run it on my own payroll?
Yes, once the column names match. It expects an employee list with hire and last working dates, and a payslip table with a period, a basic salary and the pay elements. Point the loading cell at your own export.
Does it handle end-of-service or gratuity?
Yes — the last question works the liability employee by employee on the UAE rule: 21 days a year for the first five years, 30 after that, nothing under a year, capped at two years' salary. Change the three numbers and it follows your own jurisdiction.
Are the answers correct?
All thirty were compared with the Expected_Result_Value in the dataset's Question Bank, on pandas 2.2.3 and 3.0.5, before the file was published.
Keep going on findi360
- DATA DARE Mizan Retail: Who Works Here, and What Do They Cost? — HR & Payroll Analytics DD-HRM-001 · L1 — XLSX / ~10,000 rows Mizan Manufacturing and Logistics: Two Payrolls, One Ledger — HR & Payroll Analytics DD-HRM-002 · L2 — CSV / ~30,000 rows Mizan Group: 1,000 People, One Payroll — HR & Payroll Analytics DD-HRM-003 · L3 — Parquet / ~500,000 rows Mizan Group: Every Punch, Every Payslip — HR & Payroll Analytics DD-HRM-004 · L4 — Parquet / ~3,000,000 rows
- DATA DRILL Test yourself — Power BI practice questions One question at a time, or a 50-question timed simulation.
- GUIDE Payroll analysis in Python with pandas The same work in Python, with tested code and its real output.
- COURSE Courses and free recordings Power BI, Python and finance courses for finance professionals.
Related models
-
Trial Balance in Python — Jupyter Notebook
The general ledger worked end to end in pandas: a trial balance that proves it balances, a profit and loss account by reporting line, actuals against budget, and a gross profit bridge that explains why profit grew less than sales.
-
Accounts Payable in Python — Jupyter Notebook
The purchase ledger worked end to end: what is owed at any date, how much of it is overdue, how long suppliers really wait for their money, and the invoices that were posted twice and are still sitting in the balance.
-
AR Analysis in Python — Jupyter Notebook
A complete receivables working file in pandas: load four file formats, append the new quarter, build six working tables, then answer forty graded questions — from the ageing bucket to the overdue book corrected for cash nobody could place.
Need a model we have not built yet?
Tell us what you need. Members vote, and the most-requested models are built first — free to download.
Educational model on illustrative data. Not investment, accounting or tax advice. © findi360 — share freely with attribution.
