我开发了具有多个列的df,并希望根据布尔值将图标'green_tick.jpg'和'red_cross.jpg'添加到最后2个列中
df的最后4列
这是我为将df显示到Qtableview中并插入图标而构建的类。
Class TableModel(QtCore.QAbstractTableModel):
def __init__(self, data):
super(TableModel, self).__init__()
self._data = data
def data(self, index, role):
if role == Qt.DisplayRole:
value = self._data.iloc[index.row(), index.column()]
return str(value)
if role == Qt.DecorationRole:
value = self._data[index.row()][index.column()]
if isinstance(value, bool):
if value :
return QtGui.QPixmap("green_tick.jpg")
return QtGui.QPixmap("red_cross.jpg")
def rowCount(self, index):
return self._data.shape[0]
def columnCount(self, index):
return self._data.shape[1]
def headerData(self, section, orientation, role):
# section is the index of the column/row.
if role == Qt.DisplayRole:
if orientation == Qt.Horizontal:
return str(self._data.columns[section])
if orientation == Qt.Vertical:
return str(self._data.index[section])
但是,它不断崩溃,并显示如下错误消息:
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "c:\Users\andyt\Desktop\Programming\Project\Project_4\main.py", line 265, in
in data
value = self._data[index.row()][index.column()]
File "C:\Users\andyt\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\frame.py",
line 2800, in __getitem__
indexer = self.columns.get_loc(key)
File "C:\Users\andyt\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\indexes\basse.py", line 2648, in get_loc
return self._engine.get_loc(self._maybe_cast_indexer(key))
File "pandas\_libs\index.pyx", line 111, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\index.pyx", line 138, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\hashtable_class_helper.pxi", line 1618, in pandas._libs.hashtable.PyObjectHashTablle.get_item
File "pandas\_libs\hashtable_class_helper.pxi", line 1626, in pandas._libs.hashtable.PyObjectHashTablle.get_item
KeyError: 6
有人知道为什么以及如何解决吗?
最终,我将True和False转换为字符串,然后应用以下代码作为解决方案,因为我相信带有isinstance的行会导致问题。
class TableModel(QtCore.QAbstractTableModel):
def __init__(self, data, criticaltime):
super(TableModel, self).__init__()
self.criticaltime = criticaltime
self._data = data
def data(self, index, role):
if role == Qt.DisplayRole:
value = self._data.iloc[index.row(), index.column()]
return str(value)
if role == Qt.DecorationRole:
value = self._data.iloc[index.row(), index.column()]
if value == 'In Stock':
return QtGui.QIcon(QtGui.QPixmap("C:\\Users\\andyt\\Desktop\\Programming\\Project\\Project_4\\green_tick.jpg"))
elif value == 'Out of Stock':
if datetime.datetime.strptime(str(self.criticaltime), "%Y-%m-%d") > datetime.datetime.strptime(str(self._data['EAD'].iloc[index.row()]), "%Y-%m-%d"):
return QtGui.QIcon(QtGui.QPixmap("C:\\Users\\andyt\\Desktop\\Programming\\Project\\Project_4\\warning.png"))
return QtGui.QIcon(QtGui.QPixmap("C:\\Users\\andyt\\Desktop\\Programming\\Project\\Project_4\\red_cross.jpg"))
def rowCount(self, index):
return self._data.shape[0]
def columnCount(self, index):
return self._data.shape[1]
def headerData(self, section, orientation, role):
# section is the index of the column/row.
if role == Qt.DisplayRole:
if orientation == Qt.Horizontal:
return str(self._data.columns[section])
if orientation == Qt.Vertical:
return str(self._data.index[section])