Python(熊猫)错误IndexError:单个位置索引器超出范围
Posted
技术标签:
【中文标题】Python(熊猫)错误IndexError:单个位置索引器超出范围【英文标题】:Python (Pandas) Error IndexError: single positional indexer is out-of-bounds 【发布时间】:2018-07-05 09:03:19 【问题描述】:这是我似乎无法解决的错误,将我的计数降低到比我的实际行少一可以修复它,但这意味着它甚至无法读取最后一行。错误来自我试图解析保存在同一目录中的 .csv 中的数据。
这是似乎导致问题的代码:
margin1 = datetime.timedelta(days = 1)
margin3 = datetime.timedelta(days = 3)
margin7 = datetime.timedelta(days = 7)
df = pd.read_csv('gameDB.csv')
a = df.values
rows=len(df.index)
while (x <= rows):
print (rows)
print (x)
input("Press Enter to continue...")
csvName = str((df.iloc[x,0]))
csvRel = str((df.iloc[x,1]))
csvCal = str((df.iloc[x,2]))
from datetime import datetime
today = datetime.strptime(twiday, '%Y-%m-%d').date()
compDate = datetime.strptime(csvRel, '%Y-%m-%d').date()
print (csvName + ' ' + csvRel + ' ' + csvCal)
try:
if (today+margin7 == compDate):
#tweet = (csvName + ' releases in 7 days. Click here to add to calendar ' + csvCal)
#api.update_status(tweet)
time.sleep(10)
elif (today+margin3 == compDate):
#tweet = (csvName + ' releases in 3 days. Click here to add to calendar ' + csvCal)
#api.update_status(tweet)
time.sleep(10)
elif (today+margin1 == compDate):
#tweet = (csvName + ' releases in tomorrow. Click here to add to calendar ' + csvCal)
#api.update_status(tweet)
time.sleep(10)
elif (today == compDate):
#tweet = (csvName + ' is now released.')
#api.update_status(tweet)
time.sleep(10)
except:
continue
x += 1
这是我得到的错误
Traceback (most recent call last):
File ".\gameRelease.py", line 306, in <module>
NintendoSwitch()
File ".\gameRelease.py", line 277, in NintendoSwitch
main(system,data,color,calID)
File ".\gameRelease.py", line 270, in main
twitUpdate(tDay)
File ".\gameRelease.py", line 97, in twitUpdate
csvName = str((df.iloc[x,0]))
File "C:\Users\UmbraTytan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pandas\core\indexing.py", line 1367, in __getitem__
return self._getitem_tuple(key)
File "C:\Users\UmbraTytan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pandas\core\indexing.py", line 1737, in _getitem_tuple
self._has_valid_tuple(tup)
File "C:\Users\UmbraTytan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pandas\core\indexing.py", line 204, in _has_valid_tuple
if not self._has_valid_type(k, i):
File "C:\Users\UmbraTytan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pandas\core\indexing.py", line 1672, in _has_valid_type
return self._is_valid_integer(key, axis)
File "C:\Users\UmbraTytan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pandas\core\indexing.py", line 1713, in _is_valid_integer
raise IndexError("single positional indexer is out-of-bounds")
IndexError: single positional indexer is out-of-bounds
【问题讨论】:
如何创建最小、完整和可验证的示例:***.com/help/mcve 你能显示代码吗? 好的,我可以上传所有的,一秒钟 添加了更多信息 【参考方案1】:在应用程序启动时创建 csv 时忘记添加标题行,这解决了所有问题。
writer.writeheader()
这就是它所需要的。
【讨论】:
【参考方案2】:这只是说您的 iloc 语句之一正在查找不存在的内容。如果您的 DataFrame 是 5 行长, iloc[5, 0] 会超出范围。这是因为最后一行是 iloc[4, 0],因为它从 0 开始计数。
【讨论】:
以上是关于Python(熊猫)错误IndexError:单个位置索引器超出范围的主要内容,如果未能解决你的问题,请参考以下文章
IndexError:列表索引超出范围 - python 错误
Python常见错误:IndexError: list index out of range
如何在 Python 中调试“IndexError:标量变量的无效索引”错误?