如何在python中修复'IndexError: single positional indexer is out-of-bounds'?



我正在尝试使用GUI的程序,该程序通过Excel文件。 用户将获得写入Excel文件中的数据,如果他按"接受",则应将此数据定向到一个表中。如果不是 - 另一个表。我试图以我在堆栈溢出中找到的任何方式来解决它,但这些似乎都不能解决我的问题。我希望得到任何帮助。

我一直遇到以下问题

Exception in Tkinter callback
Traceback (most recent call last):
File "C:UsersronizAppDataLocalProgramsPythonPython37libtkinter__init__.py", line 1705, in __call__
return self.func(*args)
File "C:/Users/roniz/PycharmProjects/emun/manual_validation.py", line 187, in accept_query
self.accepted.append(self.df.iloc[self.num]['bizid'])
File "C:UsersronizAppDataLocalProgramsPythonPython37libsite-packagespandascoreindexing.py", line 1424, in __getitem__
return self._getitem_axis(maybe_callable, axis=axis)
File "C:UsersronizAppDataLocalProgramsPythonPython37libsite-packagespandascoreindexing.py", line 2157, in _getitem_axis
self._validate_integer(key, axis)
File "C:UsersronizAppDataLocalProgramsPythonPython37libsite-packagespandascoreindexing.py", line 2088, in _validate_integer
raise IndexError("single positional indexer is out-of-bounds")
IndexError: single positional indexer is out-of-bounds

如果不是那个,我得到的相同只是写得不同。

我尝试将数据作为一行字典传输,我尝试将数据传输到代码的不同位置。

import tkinter as Tkinter
import googlemaps
import pandas as pd
import requests

class Application(Tkinter.Frame):
def __init__(self, master):
Tkinter.Frame.__init__(self, master)
self.parent = master
frame = Tkinter.Frame(self)
self.df = pd.read_excel('checkformanual.xlsx', sheet_name="Sheet1", header=0)
self.num = 0
self.gpi = self.df.iloc[self.num]['googleid']  # @todo: lo#
self.accepted = []
self.declined = []
self.butt1 = Tkinter.Button(frame, text='Accept', command=self.accept_query)
self.butt2 = Tkinter.Button(frame, text='Decline', command=self.decline_query)
frame.pack(fill=Tkinter.BOTH, expand=1, side=Tkinter.BOTTOM)
self.pack(fill=Tkinter.BOTH, expand=1)
def accept_query(self):
self.num += 1
self.accepted.append(self.df.iloc[self.num]['bizid'])
def decline_query(self):
self.num += 1
self.declined.append(self.df.iloc[self.num]['bizid'])
def main():
root = Tkinter.Tk()
app = Application(root)
app.mainloop()
accepteddf = app.df[app.df['bizid'].isin(app.accepted)]
declineddf = app.df[app.df['bizid'].isin(app.declined)]
accepteddf.to_excel("accepted.xlsx", index=False)
declineddf.to_excel("declined.xlsx", index=False)
if __name__ == '__main__':
main()

问题似乎出在这个函数上:

def accept_query(self):
self.num += 1
self.accepted.append(self.df.iloc[self.num]['bizid'])

具体来说,与iloc[self.num].如果提供的整数索引(在本例中为self.num(越界,此操作将引发IndexError异常。您在附加到self.accepted之前递增self.num这一事实似乎很可疑,但也许这是故意的。无论如何,如果不看到至少 excel 文件的屏幕截图,就很难进一步诊断问题。

相关内容

最新更新