In employee or project management datasets, it's common to track what project an employee started working on every week.
Instead of manually filtering week by week, Python’s Pandas library gives us an elegant and efficient way to extract:
π The initial project of every employee for each week
In this blog, I’ll walk you through how to calculate the Week (Mon–Sun) and how to extract the first project assigned in that week.
import pandas as pd
df = pd.read_excel(r"C:\Users\Vamshi Yempally\Desktop\sales.xlsx")
df.head(3)
We convert the Date column into a weekly period using .dt.to_period("W-SUN").
df['Week'] = pd.to_datetime(df['Date']).dt.to_period('W-SUN')
df.head(5)
This will give week ranges like:
2024-03-04/2024-03-10
2024-03-11/2024-03-17
Pandas automatically groups from Monday to Sunday when using 'W-SUN'.
Now the target is to get the first project the employee worked on in that week.
We use groupby() and transform('first'):
df['InitialProject'] = df.groupby(['EmployeeName','Week'])['Project'].transform('first')
df
Groups data by EmployeeName + Week
For each group, picks the first project
Applies that value to all rows of that week-group
This keeps your dataset long-format but adds a highly useful field.
Let’s check only for Surya:
s = df.query('EmployeeName == "Surya"')
s
This shows Surya’s weekly initial projects clearly.
Works perfectly for weekly summary dashboards
Helps in resource planning
Useful for team leads & managers
Accurate for time-based reporting
✔ HR Reporting
✔ Project Management
✔ Timesheet Analytics
✔ Weekly Allocation Summary
✔ Employee Productivity Tracking
0 Comments