I have the following DataFrame:
df = pd.DataFrame(
{
'OrderDayName':['Friday', 'Monday', 'Saturday', 'Sunday', 'Thursday', 'Tuesday', 'Wednesday'],
'ItemTotal':[4073.4199999999996, 6787.059999999996, 2965.2599999999984, 4416.439999999998, 4260.839999999998, 4378.229999999999, 3476.1600000000008]
}
)
I want to order the DataFrame based on the day of the week, meaning it should start with Monday and end with Sunday.
I tried copying the code in the following posts
pandas dataframe group and sort by weekday
Error: astype() got an unexpected keyword argument 'categories'
but the code below didn't work for me:
from pandas.api.types import CategoricalDtype
cats = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
cat_type = CategoricalDtype(categories=cats, ordered=True)
df['OrderDayName'] = df['OrderDayName'].astype(cat_type)
The DataFrame doesn't change - it is exactly the same as the one pictured above.
Can someone show me what I've done wrong?
Thanks
Edit: I think I got it. The following works:
cats = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
df = df.groupby(['OrderDayName']).sum().reindex(cats)
df = df.reset_index(drop=False)