pandas -使用datetimeindex对数据框进行排序 [英] Pandas - Sorting a dataframe by using datetimeindex

查看:344
本文介绍了 pandas -使用datetimeindex对数据框进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我的数据框,其中包含来自多个Excel文件的值.我想进行时间序列分析,因此将索引设为datetimeindex.但是我的索引没有按照日期排列.以下是我的数据框:

The following is my dataframe which holds values from multiple Excel files. I wanted to do a time series analysis, so I made the index as datetimeindex. But my index is not arranged according to the date. The following is my dataframe:

    Item Details    Unit    Op. Qty Price   Op. Amt.    Cl. Qty Price.1 Cl. Amt.
Month                               
2013-04-01  5 In 1  Pcs -56.0   172.78  -9675.58    -68.0   175.79  -11953.96
2013-04-01  Adaptor Pcs -17.0   9.00    -152.99 -17.0   9.00    -152.99
2013-04-01  Agro Tape   Pcs -2.0    26.25   -52.50  -2.0    26.25   -52.50
...
2014-01-01  12" Angal   Pcs -6.0    31.50   -189.00 -6.0    31.50   -189.00
2014-01-01  13 Mm Electrical Drill Check    Set -1.0    247.50  -247.50 -1.0    247.50  -247.50
2014-01-01  14" Blad    Pcs -5.0    157.49  -787.45 -5.0    157.49  -787.45
...
2013-09-01  Zinc Bolt 1/4 X 2"(box) Box -1.0    899.99  -899.99 -1.0    899.99  -899.99
2013-09-01  Zorik 88 32gram Pcs -1.0    45.00   -45.00  -1.0    45.00   -45.00
2013-09-01  Zorrik 311 Gram Pcs -1.0    270.01  -270.01 -1.0    270.01  -270.01

未按日期排序.我还想对索引及其相应的行进行排序.我用Google搜索它,发现有一种方法可以对datetimeindex进行排序,如下所示:

It is not sorted according to the date. I wanted to sort the index and its respective rows also. I googled it and found that there is a way to sort the datetimeindex and is as follows:

all_data.index.sort_values()

DatetimeIndex(['2013-04-01', '2013-04-01', '2013-04-01', '2013-04-01',
           '2013-04-01', '2013-04-01', '2013-04-01', '2013-04-01',
           '2013-04-01', '2013-04-01',
           ...
           '2014-02-01', '2014-02-01', '2014-02-01', '2014-02-01',
           '2014-02-01', '2014-02-01', '2014-02-01', '2014-02-01',
           '2014-02-01', '2014-02-01'],
          dtype='datetime64[ns]', name=u'Month', length=71232, freq=None)

但是它仅对索引进行排序,如何根据已排序的索引对整个数据框进行排序?请帮助.

But it is sorting only the index, how can I sort the entire dataframe according to the sorted index? Kindly help.

推荐答案

我认为您需要

I think you need sort_index:

all_data = all_data.sort_index()

这篇关于 pandas -使用datetimeindex对数据框进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆