pandas DataFrame对象具有sort method,可pandas DataMatrix对象没有。
按索引( date列)按升序对此DataMatrix对象进行排序的最佳方法是什么?
>>> dm
compound_ret
2/16/2011 0:00 0.006275682
2/15/2011 0:00 0.003098208
2/14/2011 0:00 0.0055039
2/13/2011 0:00 0.011471506
2/12/2011 0:00 0.011853712
2/11/2011 0:00 0.009558739
2/10/2011 0:00 0.014127912
2/9/2011 0:00 0.02042923
2/8/2011 0:00 0.023308062结果应该是DataMatrix,其中2/8/2011是第一个条目,2/16/2011是最后一个条目。compound_ret列中的条目应该跟在排序中的日期之后。因此,结果应该如下所示:
>>>dm_sorted
compound_ret
2/8/2011 0:00 0.023308062
2/9/2011 0:00 0.02042923
2/10/2011 0:00 0.014127912
2/11/2011 0:00 0.009558739
2/12/2011 0:00 0.011853712
2/13/2011 0:00 0.011471506
2/14/2011 0:00 0.0055039
2/15/2011 0:00 0.003098208
2/16/2011 0:00 0.006275682发布于 2011-06-24 22:08:43
实际上,在0.2和0.3之间,我将sortUp/sortDown重命名为单个sort方法。真对不起。
如果可以的话,我绝对建议你跟上熊猫的前沿( https://github.com/wesm/pandas )!另外,考虑在所有的交互式工作( http://ipython.scipy.org )中使用IPython --我发现制表符完成和对象的轻松自省对查找方法和探索文档字符串有很大帮助。
发布于 2011-04-06 05:44:36
你试过了吗?至少在我尝试过的熊猫版本中,DataMatrix继承了DataFrame。
>>> type(dm)
<class 'pandas.core.matrix.DataMatrix'>
>>> dm.sort()
compound_ret
2011-02-08 00:00:00 -0.6986
2011-02-09 00:00:00 0.1846
2011-02-10 00:00:00 0.2312
2011-02-11 00:00:00 1.844
2011-02-12 00:00:00 0.3662
2011-02-13 00:00:00 0.1331
2011-02-14 00:00:00 0.5166
2011-02-15 00:00:00 1.37
2011-02-16 00:00:00 0.9346
>>> dm.sort(ascending=False)
compound_ret
2011-02-16 00:00:00 0.9346
2011-02-15 00:00:00 1.37
2011-02-14 00:00:00 0.5166
2011-02-13 00:00:00 0.1331
2011-02-12 00:00:00 0.3662
2011-02-11 00:00:00 1.844
2011-02-10 00:00:00 0.2312
2011-02-09 00:00:00 0.1846
2011-02-08 00:00:00 -0.6986 https://stackoverflow.com/questions/5558607
复制相似问题