![]() |
直爽的黑框眼镜 · 应为“;”。ts(1005) ...· 6 月前 · |
![]() |
健身的脸盆 · 中国外经贸企业服务网——商务部中国企业境外商 ...· 7 月前 · |
![]() |
朝气蓬勃的薯片 · 《百川东到海》:用文学书写革命者成长的故事-新华网· 8 月前 · |
![]() |
成熟的火柴 · SqlServer中比较日期的大小_sqls ...· 1 年前 · |
![]() |
豁达的水煮鱼 · 探索未来绿色出行 ...· 1 年前 · |
pyqt5 python dataframe qtableview |
https://cloud.tencent.cn/developer/information/%E5%9C%A8qTableView%20Python%20PyQt5%E4%B8%AD%E7%BC%96%E8%BE%91%E5%90%8E%E6%9B%B4%E6%96%B0%E6%95%B0%E6%8D%AE%E5%B8%A7 |
![]() |
傻傻的课本
6 月前 |
,可以通过以下步骤实现:
table_view = QTableView()
model = QStandardItemModel()
table_view.setModel(model)
def update_dataframe():
dataframe = pd.DataFrame()
for row in range(model.rowCount()):
row_data = []
for column in range(model.columnCount()):
item = model.item(row, column)
if item is not None:
row_data.append(item.text())
else:
row_data.append('')
dataframe = dataframe.append(pd.Series(row_data), ignore_index=True)
# 在这里可以对数据帧进行进一步处理或操作
print(dataframe)
def load_dataframe(dataframe):
model.clear()
model.setColumnCount(dataframe.shape[1])
model.setRowCount(dataframe.shape[0])
for row in range(dataframe.shape[0]):
for column in range(dataframe.shape[1]):
item = QStandardItem(str(dataframe.iloc[row, column]))
model.setItem(row, column, item)
model.dataChanged.connect(update_dataframe)
完整的示例代码如下:
from PyQt5.QtWidgets import QApplication, QTableView
from PyQt5.QtGui import QStandardItemModel, QStandardItem
import pandas as pd
def update_dataframe():
dataframe = pd.DataFrame()
for row in range(model.rowCount()):
row_data = []
for column in range(model.columnCount()):
item = model.item(row, column)
if item is not None:
row_data.append(item.text())
else:
row_data.append('')
dataframe = dataframe.append(pd.Series(row_data), ignore_index=True)
# 在这里可以对数据帧进行进一步处理或操作
print(dataframe)
def load_dataframe(dataframe):
model.clear()
model.setColumnCount(dataframe.shape[1])
model.setRowCount(dataframe.shape[0])
for row in range(dataframe.shape[0]):
for column in range(dataframe.shape[1]):
item = QStandardItem(str(dataframe.iloc[row, column]))
model.setItem(row, column, item)
app = QApplication([])
table_view = QTableView()
model = QStandardItemModel()
table_view.setModel(model)
dataframe = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
load_dataframe(dataframe)
model.dataChanged.connect(update_dataframe)
table_view.show()
app.exec_()
这个例子中,我们创建了一个QTableView对象,并使用QStandardItemModel作为数据模型。通过load_dataframe函数将数据帧加载到QTableView中,然后通过update_dataframe函数将QTableView中的数据更新到数据帧中。在数据发生变化时,会触发dataChanged信号,从而调用update_dataframe函数。
![]() |
朝气蓬勃的薯片 · 《百川东到海》:用文学书写革命者成长的故事-新华网 8 月前 |