Reputation: 1035
I initially had a dataframe called df. I grouped my dataframe based on specific criteria that I had in mind using the following command.
df= df.groupby([df['Date'].dt.strftime('%B'), 'Project','Name'])['Hours'].sum()
As a result of this I got the following data frame.
**Date** **Project** **Name** **Hours**
September Project 1 x 1.00
x 3.00
Project 2 a 16.00
z 4.00
Project 3 y 1.00
June Project 1 x 1.00
x 45.00
y 6.00
July Project 2 a 16.00
z 4.00
Project 3 y 1.00
Now, I am trying to sort this dataframe based on ``Date". In other words, I want to have June, July and September rather than having September, June, July. so here is what I am hoping to get:
**Date** **Project** **Name** **Hours**
June Project 1 x 1.00
x 45.00
y 6.00
July Project 2 a 16.00
z 4.00
Project 3 y 1.00
September Project 1 x 1.00
x 3.00
Project 2 a 16.00
z 4.00
Project 3 y 1.00
Any idea how to do it?
Upvotes: 2
Views: 53
Reputation: 862511
Aggregate by months and then use rename
by dictionary:
MonthDict={ 1 : "January",
2 : "February",
3 : "March",
4 : "April",
5 : "May",
6 : "June",
7 : "July",
8 : "August",
9 : "September",
10 : "October",
11 : "November",
12 : "December"
}
df= (df.groupby([df['Date'].dt.month, 'Project','Name'])['Hours'].sum()
.rename(MonthDict, level=0))
If datetimes are sorted in original, is possible use sort=False
in groupby for avoid sorting:
df= df.groupby([df['Date'].dt.strftime('%B'), 'Project','Name'], sort=False)['Hours'].sum()
Upvotes: 1