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_())
@sameerCoder
Copy link

Great Thank you,
But i want to know how i can make a dropdown option in which options1 display first 2 rows data will and options 2 display next 2 rows and so on....
and in one columns data if that particular column data is string D then make background as green color of table cell else make background color as red ,
How i can do both the request , Thank you.

@Jhongesell
Copy link

thanks so much, your scripts is fabolous

@a12591771
Copy link

thanks so much, Your code inspired me

@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