如何按工作日名称对DataFrame进行排序?我不能使用pd.to_datetime()方法,因为我的日期不是数字。
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
答案 0 :(得分:4)
您可以将列的值转换为ordered categoricals,因此可以使用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
或按列Date
按set_index
,然后按reindex
和最后reset_index
创建索引:
通知:
解决方案仅在列的唯一值有效
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
答案 1 :(得分:0)
对分类数据使用calendar.day_name
:
from calendar import day_name
df['Date'] = pd.Categorical(df['Date'], categories=day_name, 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
如果您的文化不认为星期一为星期一,则可以将一周的天数轮换为n天。例如:
from collections import deque
days = deque(day_name)
days.rotate(1)
print(days)
deque(['Sunday', 'Monday', 'Tuesday', 'Wednesday',
'Thursday', 'Friday', 'Saturday'])
然后将categories=days
作为pd.Categorical
的参数。