Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save DataSolveProblems/972884bb9a53d5b2598e8674acc9e8ab to your computer and use it in GitHub Desktop.
Save DataSolveProblems/972884bb9a53d5b2598e8674acc9e8ab to your computer and use it in GitHub Desktop.
import sys
import pandas as pd
from PyQt5.QtWidgets import QApplication, QTableView
from PyQt5.QtCore import QAbstractTableModel, Qt
df = pd.DataFrame({'a': ['Mary', 'Jim', 'John'],
'b': [100, 200, 300],
'c': ['a', 'b', 'c']})
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, parnet=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
def headerData(self, col, orientation, role):
if orientation == Qt.Horizontal and role == Qt.DisplayRole:
return self._data.columns[col]
return None
if __name__ == '__main__':
app = QApplication(sys.argv)
model = pandasModel(df)
view = QTableView()
view.setModel(model)
view.resize(800, 600)
view.show()
sys.exit(app.exec_())
@majuch
Copy link

majuch commented Dec 10, 2020

thanks so much 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment