25

以下是我的数据框,其中包含来自多个 Excel 文件的值。我想做一个时间序列分析,所以我将索引设为datetimeindex. 但是我的索引不是按日期排列的。以下是我的数据框:

    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进行排序,如下:

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)

但它只是对索引进行排序,如何根据排序索引对整个数据框进行排序?请帮忙。

4

1 回答 1

61

我认为你需要sort_index

all_data = all_data.sort_index()
于 2016-10-26T12:49:17.497 回答