0

I am very new to PyQT and stumbled upon the following obstacle. I have created a pandas DataFrame and managed to visualize it in a pyqt4 Table View with some code I found in another thread (Fastest way to populate QTableView from Pandas data frame). Now I want to highlight the highest value in each row.

Currently the code that I'm using for loading a pandas DataFrame into the Table View looks like this:

class PandasModel(QtCore.QAbstractTableModel):
    """
    Class to populate a table view with a pandas dataframe
    """
    def __init__(self, data, parent=None):
        QtCore.QAbstractTableModel.__init__(self, parent)
        self._data = data

    def rowCount(self, parent=None):
        return len(self._data.values)

    def columnCount(self, parent=None):
        return self._data.columns.size

    def data(self, index, role):
        if index.isValid():
            if role == QtCore.Qt.DisplayRole:
                return str(self._data.values[index.row()][index.column()])
        return None

    def headerData(self, col, orientation, role):
        if orientation == QtCore.Qt.Horizontal and role == QtCore.Qt.DisplayRole:
            return self._data.columns[col]
        return None

I have no idea whether what I want to achieve is possible using this method, since I don't think I understand the PyQT structure well enough, so any solution is welcome.

The desired result I have in mind would finally look like this for example

Thank you in advance!

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
David
  • 3
  • 4

1 Answers1

0

enter image description here

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QTreeWidgetItem
import sys
import pandas as pd
import numpy as np

class PandasModel(QtCore.QAbstractTableModel):
    """
    Class to populate a table view with a pandas dataframe
    """
    def __init__(self, data, parent=None):
        QtCore.QAbstractTableModel.__init__(self, parent)
        self._data = data

    def rowCount(self, parent=None):
        return len(self._data.values)

    def columnCount(self, parent=None):
        return self._data.columns.size

    def data(self, index, role):
        if index.isValid():
            if role == QtCore.Qt.DisplayRole:
                return str(self._data.values[index.row()][index.column()])
            if role == QtCore.Qt.BackgroundColorRole:
                row = index.row()
                col = index.column()
                if self._data.iloc[row,col] == self._data.iloc[row].max():
                    color = QtGui.QColor('red')
                else:
                    color = QtGui.QColor('white')
                pixmap = QtGui.QPixmap(26, 26)
                pixmap.fill(color)
                return color
        return None

    def headerData(self, col, orientation, role):
        if orientation == QtCore.Qt.Horizontal and role == QtCore.Qt.DisplayRole:
            return self._data.columns[col]
        return None

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    df = pd.DataFrame(np.random.randn(8,3).round(3))

    view = QtWidgets.QTableView()
    view.setModel(PandasModel(df))

    view.show()
    sys.exit(app.exec_())
pyjamas
  • 4,608
  • 5
  • 38
  • 70