Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

You can convert the string value to an integer by using the astype method of the Pandas DataFrame. Here's an example:

import pandas as pd
from PyQt5.QtCore import QAbstractTableModel, Qt

class PandasTableModel(QAbstractTableModel):
    def __init__(self, data):
        super().__init__()
        self._data = data

    def data(self, index, role):
        if role == Qt.DisplayRole:
            value = self._data.iloc[index.row(), index.column()]
            if isinstance(value, str):
                return int(value)  # here we convert the string value to an integer
            return value

    def rowCount(self, parent):
        return len(self._data.index)

    def columnCount(self, parent):
        return len(self._data.columns)

    def headerData(self, section, orientation, role):
        if role != Qt.DisplayRole:
            return None
        if orientation == Qt.Horizontal:
            return self._data.columns[section]
        else:
            return str(section)

In this example, we check if the value is a string using the isinstance method. If the value is a string, we convert it to an integer using the int function before returning it. Otherwise, we return the original value. This should allow you to display string values as integers in your PandasTableModel.