-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpandas_to_pyqt_table.py
61 lines (49 loc) · 1.88 KB
/
pandas_to_pyqt_table.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# coding=utf-8
from PyQt5 import QtCore
from PyQt5 import QtWidgets
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=QtCore.Qt.DisplayRole):
if index.isValid():
# column마다 type 다를 경우 여기를 수정
if role == QtCore.Qt.DisplayRole:
if (index.column() == 0):
return str(self._data.values[index.row()][index.column()])
else:
return str(self._data.values[index.row()][index.column()])
return None
def headerData(self, section, orientation, role):
if orientation == QtCore.Qt.Horizontal and role == QtCore.Qt.DisplayRole:
return self._data.columns[section]
elif orientation == QtCore.Qt.Vertical and role == QtCore.Qt.DisplayRole:
return str(self._data.index[section])
return None
def flags(self, index):
flags = super(self.__class__, self).flags(index)
flags |= QtCore.Qt.ItemIsSelectable
flags |= QtCore.Qt.ItemIsEnabled
return flags
# usage
if __name__ == '__main__':
import pandas as pd
import numpy as np
df = pd.DataFrame()
df['Field1'] = np.arange(0, 10, .5)
df['Field2'] = np.arange(0, 10, .5)
app = QtWidgets.QApplication([])
table = QtWidgets.QTableView()
mymodel = PandasModel(df)
table.setSelectionMode(QtWidgets.QTableView.MultiSelection)
table.setSelectionBehavior(QtWidgets.QTableView.SelectRows)
table.setModel(mymodel)
table.show()
app.exec_()