按升序对 pandas DataMatrix 进行排序

发布于 2024-10-30 13:00:31 字数 980 浏览 1 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最冷一天 2024-11-06 13:00:31

事实上,在 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 single sort 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.

人生百味 2024-11-06 13:00:31

你尝试过吗?至少在我尝试的 pandas 版本中,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         

Did you try it? At least in the version of pandas I tried, DataMatrix inherits from 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         
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文