按升序对 pandas DataMatrix 进行排序
pandas DataFrame 对象有一个 排序方法< /a> 但 pandas DataMatrix 对象没有。
按索引(日期列)升序排序此 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
The pandas DataFrame object has a sort method but pandas DataMatrix object does not.
What is the best way to sort this DataMatrix object by index (the date column) in ascending order?
>>> 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
The result should be the DataMatrix with 2/8/2011 as the first entry and 2/16/2011 as the last entry. The entries in the compound_ret column should follow their date in the sort. So the result should look something like this:
>>>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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
事实上,在 0.2 和 0.3 之间,我将
sortUp
/sortDown
重命名为单个sort
方法。对此感到抱歉。如果可以的话,我绝对建议您紧跟 pandas 的前沿(https://github.com/wesm/pandas)!另外,考虑使用 IPython 进行所有交互式工作( http://ipython.scipy.org )——我发现具有制表符补全和对象的简单内省对于查找方法和探索文档字符串有很大帮助。
Indeed between 0.2 and 0.3 I renamed
sortUp
/sortDown
to the singlesort
methods. Sorry about that.I definitely recommend keeping up on the bleeding edge of pandas if you can ( https://github.com/wesm/pandas )! Also, consider using IPython for all your interactive work ( http://ipython.scipy.org )-- I find that having tab completion and easy introspection of objects helps a great deal for finding methods and exploring docstrings.
你尝试过吗?至少在我尝试的 pandas 版本中,DataMatrix 继承自 DataFrame。
Did you try it? At least in the version of pandas I tried,
DataMatrix
inherits fromDataFrame
.