πŸ“˜ How to Get Week-Wise Initial Project of Employees in Python Using Pandas | Datawiz Vamshi

 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.


πŸ” Step 1: Load the Dataset

import pandas as pd df = pd.read_excel(r"C:\Users\Vamshi Yempally\Desktop\sales.xlsx") df.head(3)

πŸ“† Step 2: Create Week Column (Mon–Sun)

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'.


🧠 Step 3: Extract Week-Wise Initial Project

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

✔ What this does:

  • 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.


πŸ‘€ Step 4: Filter Results for a Specific Employee

Let’s check only for Surya:

s = df.query('EmployeeName == "Surya"') s

This shows Surya’s weekly initial projects clearly.


🧩 Why This Method is Powerful?

  • Works perfectly for weekly summary dashboards

  • Helps in resource planning

  • Useful for team leads & managers

  • Accurate for time-based reporting


πŸš€ Real-World Use Cases

✔ HR Reporting
✔ Project Management
✔ Timesheet Analytics
✔ Weekly Allocation Summary
✔ Employee Productivity Tracking

Post a Comment

0 Comments