当我使用几列(['Symbol','Year','Month','Day']
)对我的 DataFrame 进行排序时,生成的 DataFrame 是按Symbol > Year > Month
但不是按以下方式排序的Day
:
In [1]: df = pd.DataFrame({'Symbol': {79: 'F', 81: 'F', 82: 'F', 83: 'F', 84: 'F', 85: 'F', 86: 'F', 87: 'F', 89: 'F'}, 'Shares': {79: 100, 81: 100, 82: 100, 83: 100, 84: 100, 85: 100, 86: 100, 87: 100, 89: 100}, 'Month': {79: '08', 81: '08', 82: '08', 83: '08', 84: '08', 85: '08', 86: '08', 87: '08', 89: '09'}, 'Year': {79: '2008', 81: '2008', 82: '2008', 83: '2008', 84: '2008', 85: '2008', 86: '2008', 87: '2008', 89: '2008'}, 'Action': {79: 'Sell', 81: 'Sell', 82: 'Buy', 83: 'Sell', 84: 'Buy', 85: 'Sell', 86: 'Buy', 87: 'Sell', 89: 'Sell'}, 'Day': {79: 2L, 81: 4L, 82: '06', 83: 11L, 84: '13', 85: 18L, 86: '18', 87: 23L, 89: 22L}})
In [2]: df
Out[2]:
Action Day Month Shares Symbol Year
79 Sell 2 08 100 F 2008
81 Sell 4 08 100 F 2008
82 Buy 06 08 100 F 2008
83 Sell 11 08 100 F 2008
84 Buy 13 08 100 F 2008
85 Sell 18 08 100 F 2008
86 Buy 18 08 100 F 2008
87 Sell 23 08 100 F 2008
89 Sell 22 09 100 F 2008
In [3]: df.sort(['Symbol','Year','Month','Day'])
Out[3]:
Action Day Month Shares Symbol Year
79 Sell 2 08 100 F 2008
81 Sell 4 08 100 F 2008
83 Sell 11 08 100 F 2008
85 Sell 18 08 100 F 2008
87 Sell 23 08 100 F 2008
82 Buy 06 08 100 F 2008
84 Buy 13 08 100 F 2008
86 Buy 18 08 100 F 2008
89 Sell 22 09 100 F 2008
为什么没有sort
按预期工作?