pandas中的DataFrame有以下基本属性:
属性 | 描述 | |
1 | values | 返回ndarray类型对象 |
2 | index | 行索引 |
3 | columns | 列索引 |
4 | axes | 行和列索引 |
5 | T | 行和列对换 |
6 | info | 对象信息 |
7 | head(i) | 显示前i行数据 |
8 | tail(i) | 显示后i行数据 |
9 | describe() | 数据按列的统计信息 |
10 | DataFrame() | 初始化 |
创建一个df数据:
data = {'性别': ['男', '女', '女', '男', '男'],
'姓名': ['小明', '小红', '小芳', '小强', '小美'],
'年龄': [20, 21, 25, 24, 29]}
df = pd.DataFrame(data, index=['No.1', 'No.2', 'No.3', 'No.4', 'No.5'],
columns=['姓名', '性别', '年龄', '职业'])
创建一个PyQt基于QAbstractTableModel显示数据类model:
class PdTable(QAbstractTableModel):
def __init__(self, data):
QAbstractTableModel.__init__(self)
self._data = data
def rowCount(self, parent=None):
return self._data.shape[0]
def columnCount(self, parent=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]
elif orientation == Qt.Vertical and role == Qt.DisplayRole:
return self._data.axes[0][col]
return None
效果如下:
完整代码如下:
"""
有趣的事情
没有结束
2020/4/17 10:14
"""
import pandas as pd
from PyQt5.QtWidgets import (QApplication, QTableView)
from PyQt5.QtCore import (QAbstractTableModel, Qt)
class PdTable(QAbstractTableModel):
def __init__(self, data):
QAbstractTableModel.__init__(self)
self._data = data
def rowCount(self, parent=None):
return self._data.shape[0]
def columnCount(self, parent=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]
elif orientation == Qt.Vertical and role == Qt.DisplayRole:
return self._data.axes[0][col]
return None
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
data = {'性别': ['男', '女', '女', '男', '男'],
'姓名': ['小明', '小红', '小芳', '小强', '小美'],
'年龄': [20, 21, 25, 24, 29]}
df = pd.DataFrame(data, index=['No.1', 'No.2', 'No.3', 'No.4', 'No.5'],
columns=['姓名', '性别', '年龄', '职业'])
model = PdTable(df)
view = QTableView()
view.setModel(model)
view.setWindowTitle('Pandas')
view.resize(410, 250)
view.setAlternatingRowColors(True)
view.show()
sys.exit(app.exec_())
多谢,美。