按工作日排序熊猫数据框

时间:2018-11-07 12:10:32

标签: python python-3.x pandas sorting dataframe

如何按工作日名称对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

2 个答案:

答案 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

或按列Dateset_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的参数。