I want to display dataframe in Pyqt5. However, I get an error like 'Process finished with exit code -1073740791 (0xC0000409)'
Open csv file with pandas
csv_path = os.path.expanduser("~/Desktop/dataset/traffic-crashes-vehicles-1.csv")
datac = pd.read_csv(csv_path)
class pandasModel(QAbstractTableModel):
def __init__(self,data):
QAbstractTableModel.__init__(self)
self._data = data
def rowCount(self,parent=None):
return self._data.shape[0]
def columnCount(self,parent=None):
return self._data.shape[1]
def data(self, index, role=Qt.DisplayRole):
if index.isValid():
if role == Qt.DisplayRole:
return str(self._data.iloc[index.row(),index.column()])
return None
the function causing the problem header Data function. Parameters after self show in yellow. PyCharm. Something seems missing or unidentifiable.
def headerData(self,orientation, role,col):
if orientation == Qt.Horizantal and role == Qt.DisplayRole:
return self._data.columns[col]
return None
if __name__ == '__main__':
app = QApplication(sys.argv)
model = pandasModel(datac)
view = QTableView()
view.setModel(model)
view.resize(800,600)
view.show()
sys.exit(app.exec())
I replaced col with seciton. I printed this section and it worked as much as my column count and printed a numerical value. I understood that section is counting columns. To get the columns from dataframe, I wrote a for loop and added the section. And now my column names appear.
def headerData(self, section, orientation, role=Qt.DisplayRole):
if orientation == Qt.Horizontal and role == Qt.DisplayRole:
return datac.columns[section]
return QAbstractTableModel.headerData(self, section, orientation, role)
User contributions licensed under CC BY-SA 3.0