You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

关于pandas中出现IndexingError: Too many indexers错误的技术问询

解决pandas中"Too many indexers"索引错误

错误原因

Series是一维数据结构,但你在loc中传入了多个逗号分隔的布尔值参数(True,False,False,True,True),pandas会将这些参数解读为对应多维数据的多个索引器,而一维Series无法识别这么多索引器,因此抛出Too many indexers错误。

修正方案

要实现按布尔筛选Series的需求,需要把布尔值包装成一个列表传入loc,这样pandas会将其识别为一维的布尔掩码进行筛选。

修正后的代码

import pandas as pd
s = pd.Series([10,20,30,40,50])
print(s.loc[[True,False,False,True,True]])

运行结果

0    10
3    40
4    50
dtype: int64

报错信息(中文翻译)

C:/Users/Dell/AppData/Local/Programs/Python/Python39/python.exe "c:/Users/Dell/Desktop/Procx flask/test.py"
回溯(最近的调用最后):
  文件 "c:\Users\Dell\Desktop\Procx flask\test.py",第5行,在<module>中
    print(s.loc[True,False,False,True,True])
  文件 "C:\Users\Dell\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\indexing.py",第925行,在__getitem__中
    return self._getitem_tuple(key)
  文件 "C:\Users\Dell\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\indexing.py",第1103行,在_getitem_tuple中
    self._has_valid_tuple(tup)
  文件 "C:\Users\Dell\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\indexing.py",第751行,在_has_valid_tuple中
    self._validate_key_length(key)
  文件 "C:\Users\Dell\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\indexing.py",第792行,在_validate_key_length中
    raise IndexingError("Too many indexers")
pandas.core.indexing.IndexingError: 索引器过多

内容的提问来源于stack exchange,提问作者KIRAN KUMAR N

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 13:42:46