重新索引或重新排序组

时间:2012-11-02 01:41:04

标签: python pandas

我正在寻找一种干净的方法来重新排序组中的索引 示例代码:

import numpy as np
import pandas as pd

mydates = pd.date_range('1/1/2012', periods=1000, freq='D')
myts = pd.Series(np.random.randn(len(mydates)), index=mydates)
grouped = myts.groupby(lambda x: x.timetuple()[7])
mymin = grouped.min()
mymax = grouped.max()

以上给了我想要的东西,汇总了一年中朱利安日的统计数据但是我想重新排序这组,所以下半场(183天)放在上半场前面。 使用正常的numpy数组:

myindex = np.arange(1,367)
myindex = np.concatenate((myindex[183:],myindex[:183]))

但我无法通过群组执行此操作,因为它会引发一个非实现错误。

注意:这是来自google-groups的交叉帖子。我也一直在阅读comp.lang.python,遗憾的是人们往往会忽略一些帖子,例如来自谷歌小组。

提前致谢,
贝文

2 个答案:

答案 0 :(得分:6)

为什么不重新索引结果?

In [7]: mymin.reindex(myindex)
Out[7]: 
184   -0.788140
185   -2.206314
186    0.284884
187   -2.197727
188   -0.714634
189   -1.082745
190   -0.789286
191   -1.489837
192   -1.278941
193   -0.795507
194   -0.661476
195    0.582994
196   -1.634310
197    0.104332
198   -0.602378
...
169   -1.150616
170   -0.315325
171   -2.233139
172   -1.081528
173   -1.316668
174   -0.963783
175   -0.215260
176   -2.723446
177   -0.493480
178   -0.706771
179   -2.082051
180   -1.066649
181   -1.455419
182   -0.332383
183   -1.277424

答案 1 :(得分:0)

我不知道具体的Pandas功能,但您可以考虑使用np.roll()函数:

myindex = np.arange(1,367)
myindex = np.roll(myindex, int(len(myindex)/2.))
相关问题