2017-07-02 58 views
1

我有一系列整数,它是未排序的,索引为1..2000。如果我对这个系列进行排序,那么这些值就会被排序,但它们会保留旧的索引(这是有道理的)。我的问题是,我如何重新编制这个系列的索引,所以1是最小值的索引,2是第二小的索引等等?重新索引排序系列

为了说明,我的出发系列(长4为简单起见):

0: 500 
1: 249 
2: 873 
3: 90 

排序使得:

3: 90 
1: 249 
0: 500 
2: 873 

我怎样才能使上述成

0: 90 
1: 249 
2: 500 
3: 873 

回答

3

您可以致电给reset_index()

s.reset_index(inplace=True, drop=True) 

更多关于它在here

inplace : boolean, default False Modify the Series in place (do not create a new object)

drop : boolean, default False Do not try to insert index into dataframe columns

实现它的就地参数的影响,只有当drop=True和结果保持原样系列的结果。

+0

我得到'类型错误:无法就地reset_index一个系列创建DataFrame' – Bluefire

+0

@Bluefire,你说的没错,你应该加上'下降= TRUE'作为参数。 –

+0

'>>> type(series.reset_index(inplace = True,drop = True))' 'NoneType' – Bluefire