从大型 pandas 数据帧格式化 QAbstractTableModel 中的选定列
Format selected columns in QAbstractTableModel from large pandas dataframes
下面的代码生成了一个从 Pandas DataFrame 生成的 QTableView。 A
和 B
列包含整数值,C
包含每个单元格的整数列表。
这当前显示为 int 值。我的问题是,如何使列 B
和 C
显示为十六进制值,但将列 A
保留为整数。我不想更改 if __main__
.
下的任何内容
from PyQt5 import QtCore, QtWidgets
import numpy as np
import sys
import pandas as pd
class raw_data_table_view(QtWidgets.QTableView):
def __init__(self, data):
QtWidgets.QTableView.__init__(self)
self.setSelectionMode(QtWidgets.QAbstractItemView.NoSelection)
self.horizontalHeader().setSectionResizeMode(QtWidgets.QHeaderView.ResizeToContents)
model = PandasModel(data)
self.setModel(model)
class PandasModel(QtCore.QAbstractTableModel):
def __init__(self, data, parent=None):
QtCore.QAbstractTableModel.__init__(self, parent)
self._data = np.array(data.values)
self._cols = data.columns
self.r, self.c = np.shape(self._data)
def rowCount(self, parent=None):
return self.r
def columnCount(self, parent=None):
return self.c
def data(self, index, role=QtCore.Qt.DisplayRole):
if index.isValid():
if role == QtCore.Qt.DisplayRole:
return str(self._data[index.row(), index.column()])
return None
def headerData(self, p_int, orientation, role):
if role == QtCore.Qt.DisplayRole:
if orientation == QtCore.Qt.Horizontal:
return self._cols[p_int]
elif orientation == QtCore.Qt.Vertical:
return p_int
return None
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
length = 300000
df = pd.DataFrame(np.random.randint(0, 100, size=(length, 2)),
columns=list('AB'))
df['C'] = list(np.random.randint(0, 255, size=(length, 8)))
window = raw_data_table_view(df)
window.resize(400, 800)
window.show()
sys.exit(app.exec_())
您可以通过index
参数select相关列,然后在返回数据时根据需要格式化列值:
def data(self, index, role=QtCore.Qt.DisplayRole):
if index.isValid():
if role == QtCore.Qt.DisplayRole:
column = index.column()
data = self._data[index.row(), index.column()]
if column == 1:
return hex(data)
elif column == 2:
with np.printoptions(formatter={'int':hex}):
return str(data)
else:
return data
return None
结果:
下面的代码生成了一个从 Pandas DataFrame 生成的 QTableView。 A
和 B
列包含整数值,C
包含每个单元格的整数列表。
这当前显示为 int 值。我的问题是,如何使列 B
和 C
显示为十六进制值,但将列 A
保留为整数。我不想更改 if __main__
.
from PyQt5 import QtCore, QtWidgets
import numpy as np
import sys
import pandas as pd
class raw_data_table_view(QtWidgets.QTableView):
def __init__(self, data):
QtWidgets.QTableView.__init__(self)
self.setSelectionMode(QtWidgets.QAbstractItemView.NoSelection)
self.horizontalHeader().setSectionResizeMode(QtWidgets.QHeaderView.ResizeToContents)
model = PandasModel(data)
self.setModel(model)
class PandasModel(QtCore.QAbstractTableModel):
def __init__(self, data, parent=None):
QtCore.QAbstractTableModel.__init__(self, parent)
self._data = np.array(data.values)
self._cols = data.columns
self.r, self.c = np.shape(self._data)
def rowCount(self, parent=None):
return self.r
def columnCount(self, parent=None):
return self.c
def data(self, index, role=QtCore.Qt.DisplayRole):
if index.isValid():
if role == QtCore.Qt.DisplayRole:
return str(self._data[index.row(), index.column()])
return None
def headerData(self, p_int, orientation, role):
if role == QtCore.Qt.DisplayRole:
if orientation == QtCore.Qt.Horizontal:
return self._cols[p_int]
elif orientation == QtCore.Qt.Vertical:
return p_int
return None
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
length = 300000
df = pd.DataFrame(np.random.randint(0, 100, size=(length, 2)),
columns=list('AB'))
df['C'] = list(np.random.randint(0, 255, size=(length, 8)))
window = raw_data_table_view(df)
window.resize(400, 800)
window.show()
sys.exit(app.exec_())
您可以通过index
参数select相关列,然后在返回数据时根据需要格式化列值:
def data(self, index, role=QtCore.Qt.DisplayRole):
if index.isValid():
if role == QtCore.Qt.DisplayRole:
column = index.column()
data = self._data[index.row(), index.column()]
if column == 1:
return hex(data)
elif column == 2:
with np.printoptions(formatter={'int':hex}):
return str(data)
else:
return data
return None
结果: