ValueError:索引必须是单调递增或递减 [英] ValueError: index must be monotonic increasing or decreasing
问题描述
ser3 = Series(['USA','Mexico','Canada'],index = ['0','5','10'])
此处ranger = range(15)
在iPython中使用正向填充时出现错误
I get an error while using Forward fill in iPython
ser3.reindex(ranger,method = 'ffill')
/Users/varun/anaconda/lib/python2.7/site-packages/pandas/core/index.pyc in _searchsorted_monotonic(self, label, side)
2395 return len(self) - pos
2396
-> 2397 raise ValueError('index must be monotonic increasing or decreasing')
2398
2399 def get_slice_bound(self, label, side, kind):
ValueError: index must be monotonic increasing or decreasing
推荐答案
正如David所说,这是由于index是字符串.但是,为什么会出现索引不是单调错误"的问题,答案是-为了使重新编制索引的方法起作用,您的索引必须按排序/单调/递增的顺序进行.而且当您的索引是字符串时,它没有进行排序,因此应该进行正确的排序:
As David said it was due to index being a string. But why were you getting the "Index not monotonic Error" and the answer to that is - For reindexing methods to work, your index must be in sorted/monotonic/increasing order. And when your index was a string, it wasn't sorted, correct sorting should have been:
ser3 =系列(['USA','Mexico','Canada'],index = ['0','10','5']) 游侠= range(15)
ser3 = Series(['USA','Mexico','Canada'],index = ['0','10','5']) ranger = range(15)
In [100]: ser3.reindex(ranger,method = 'ffill')
Out[100]:
0 NaN
1 NaN
2 NaN
3 NaN
4 NaN
5 NaN
6 NaN
7 NaN
8 NaN
9 NaN
10 NaN
11 NaN
12 NaN
13 NaN
14 NaN
dtype: object
希望这会有所帮助,并使重新索引更加清晰!!
Hope this helps and makes reindex clearer !!
这篇关于ValueError:索引必须是单调递增或递减的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!