Created
April 17, 2019 03:28
-
-
Save DataSolveProblems/972884bb9a53d5b2598e8674acc9e8ab to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks so much 👍