gpt4 book ai didi

numpy - 值错误 : index must be monotonic increasing or decreasing

转载 作者:行者123 更新时间:2023-12-02 04:19:53 25 4
gpt4 key购买 nike

ser3 = Series(['美国','墨西哥','加拿大'],index = ['0','5','10'])

此处 ranger = range(15)

我在 iPython 中使用 Forward fill 时出错

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

最佳答案

正如大卫所说,这是因为索引是一个字符串。但是,为什么您会收到“索引不是单调错误”,而答案是-要使重新索引方法起作用,您的索引必须按排序/单调/递增顺序排列。当你的索引是一个字符串时,它没有被排序,正确的排序应该是:

ser3 = 系列 (['美国','墨西哥','加拿大'],index = ['0','10','5'])护林员 = 范围 (15)

注意:ranger 是一个整数序列,而 index 是字符串序列,该方法不会做太多但 reindex 会起作用

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

希望这有助于并使重建索引更清晰!!

关于numpy - 值错误 : index must be monotonic increasing or decreasing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31285508/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com