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.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2023-05-28 12:12:35 +0000
Seen: 10 times
Last updated: May 28 '23
How can I set up Gunicorn with a Django Project?
Looking for a Python Module that finds Tags for a Text describing its Content
Need a Function in Python to remove entries less than 2 digits from an Array
How can I convert a Document in Python?
How can I program a Loop in Python?
How can I enable Python Code Highlighting in Askbot?