按升序对 Pandas DataMatrix 进行排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5558607/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 15:38:22  来源:igfitidea点击:

Sort a pandas DataMatrix in ascending order

pythonsortingnumpypandas

提问by Jason Strimpel

The pandas DataFrame object has a sort methodbut pandas DataMatrix object does not.

pandas DataFrame 对象有一个sort 方法,但 pandas DataMatrix 对象没有。

What is the best way to sort this DataMatrix object by index (the date column) in ascending order?

按索引(日期列)按升序排序此 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

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:

结果应该是 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

回答by Wes McKinney

Indeed between 0.2 and 0.3 I renamed sortUp/sortDownto the single sortmethods. Sorry about that.

实际上在 0.2 和 0.3 之间,我将sortUp/重命名sortDown为单个sort方法。对于那个很抱歉。

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.

如果可以的话,我绝对建议您跟上熊猫的前沿(https://github.com/wesm/pandas)!此外,考虑将 IPython 用于所有交互式工作 ( http://ipython.scipy.org)——我发现制表符完成和对象的轻松自省对查找方法和探索文档字符串有很大帮助。

回答by Nicholas Riley

Did you try it? At least in the version of pandas I tried, DataMatrixinherits from DataFrame.

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