當我使用排序若干列(['Symbol','Year','Month','Day']
)所產生的數據幀由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
按預期工作?
您是否可以編輯您的文章以包含您擁有的最小再現案例(以及提及您使用的是哪個版本的熊貓)? – DSM
據此編輯。謝謝。 –
看起來像你正在使用不同的名稱進行排序比在你的列標題 – zach