How do i sort the DataFrame by weekday names? I cannot use pd.to_datetime() method because my dates aren't numbers.
Date Transactions
0 Friday 140.652174
1 Monday 114.000000
2 Saturday 208.826087
3 Sunday 140.565217
4 Thursday 118.217391
5 Tuesday 107.826087
6 Wednesday 105.608696
You can convert column values to ordered categoricals, so it is possible to use sort_values
:
cats = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
df['Date'] = pd.Categorical(df['Date'], categories=cats, ordered=True)
df = df.sort_values('Date')
print (df)
Date Transactions
1 Monday 114.000000
5 Tuesday 107.826087
6 Wednesday 105.608696
4 Thursday 118.217391
0 Friday 140.652174
2 Saturday 208.826087
3 Sunday 140.565217
Or create an index from the Date
column with set_index
, then reindex
and lastly reset_index
:
Notice:
Solution only works if column values are unique
df = df.set_index('Date').reindex(cats).reset_index()
print (df)
Date Transactions
0 Monday 114.000000
1 Tuesday 107.826087
2 Wednesday 105.608696
3 Thursday 118.217391
4 Friday 140.652174
5 Saturday 208.826087
6 Sunday 140.565217
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