【发布时间】:2020-05-01 15:31:47
【问题描述】:
当尝试获取数据框中某些列的值计数时,我收到此错误消息,指出索引必须是单调的,但 is_monotonic 属性表明索引已经是单调的。导入 csv 后数据框中的大多数列不会返回此错误,但少数会返回。
我尝试了here, 提到的一些策略,但似乎无法奏效。
这样做:
import pandas as pd
data = pd.read_csv('info/train.csv')
print('Monotonic?: ', data['net_booking_value_monthly'].index.is_monotonic)
print(data['net_booking_value_monthly'].value_counts(dropna=False)[:10])
给我这个:
Monotonic?: True
Traceback (most recent call last):
File "/Users/person/venvs/science/lib/python3.6/site-packages/pandas/core/indexes/base.py", line 3484, in get_slice_bound
return self._searchsorted_monotonic(label, side)
File "/Users/person/venvs/science/lib/python3.6/site-packages/pandas/core/indexes/base.py", line 3443, in _searchsorted_monotonic
raise ValueError('index must be monotonic increasing or decreasing')
ValueError: index must be monotonic increasing or decreasing
During handling of the above exception, another exception occurred:
等等。等等
is_monotonic 属性为 True,但值计数返回此错误,这让我很头疼。输入的 CSV 文件非常大,我无法共享它,但是我应该在其中寻找什么会导致这种情况的东西吗?
Pandas 版本为 0.20.2。
【问题讨论】:
-
试试这个?
data = data.reset_index(drop=True) -
干杯。试过了。不幸的是没有骰子。完全相同的错误。
-
您介意提供准确重现您的问题的数据样本吗?您也可以尝试更新到最新的稳定版本 - 0.21。
-
好的,所以这个 csv 数据会产生错误:link 如果我输入这个:
print('Vegetables are Monotonic?: ', data['vegetables'].index.is_monotonic) print(data['vegetables'].value_counts()) print('Values are Monotonic?: ', data['value'].index.is_monotonic) print(data['value'].value_counts()[)我会得到这个:Vegetables are Monotonic?: True 4.0 48 etc. Name: vegetables, dtype: int64 ValueError: index must be monotonic奇怪的是,如果我将 CSV 缩短为 99 行,我会得到第一列(蔬菜)出现错误 -
我不这么认为。这可能是一个错误。对不起,我在我的头上。 :-)
标签: python python-3.x pandas csv