I started with this dataframe that had 4 columns: Name, Hours, Start Date and End Date:
| Name | Hours | Start Date | End Date | Total Days |
|---|---|---|---|---|
| Ashley | 6 | 1 | 2 | 2 |
| Bob | 8 | 1 | 2 | 2 |
| John | 5 | 3 | 5 | 3 |
| Chris | 5 | 4 | 4 | 1 |
Where 1-5 in the Start Date and End Date columns correspond to Monday-Friday.
I need to output a new dataframe/table that looks like this:
| Name | Monday | Tuesday | Wednesday | Thursday | Friday |
|---|---|---|---|---|---|
| Ashley | 6 | 6 | |||
| Bob | 8 | 8 | |||
| John | 5 | 5 | 5 | ||
| Chris | 5 |
I figured I’d make a dictionary to map 1 thru 5 to M thru F.
I then calculated the 4th column, "Total Days", thinking maybe I could first map the respective hours to the Starting Date and then use the total days values to have it iterate that many columns after, but I’m totally lost.
Let's try:
import calendar
df['days'] = df.apply(lambda x: np.arange(x['Start Date'], x['End Date']+1), axis=1)
dfe = df.explode('days')
df_out = dfe.pivot_table('Hours', 'Name', 'days', aggfunc='sum', fill_value=0)\
.rename(columns=dict(enumerate(calendar.day_name, 1)))
print(df_out)
Output:
days Monday Tuesday Wednesday Thursday Friday
Name
Ashley 6 6 0 0 0
Bob 8 8 0 0 0
Chris 0 0 0 5 0
John 0 0 5 5 5
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With