pandas.errors.UnsortedIndexError#

exception pandas.errors.UnsortedIndexError[source]#

当切片操作的 MultiIndex 未经过字典序排序时引发的错误。

KeyError 的子类。

另请参阅

DataFrame.sort_index

按索引对 DataFrame 进行排序。

DataFrame.set_index

使用现有列设置 DataFrame 索引。

示例

>>> df = pd.DataFrame(
...     {
...         "cat": [0, 0, 1, 1],
...         "color": ["white", "white", "brown", "black"],
...         "lives": [4, 4, 3, 7],
...     },
... )
>>> df = df.set_index(["cat", "color"])
>>> df
            lives
cat  color
0    white    4
     white    4
1    brown    3
     black    7
>>> df.loc[(0, "black") : (1, "white")]
Traceback (most recent call last):
UnsortedIndexError: 'Key length (2) was greater
than MultiIndex lexsort depth (1)'