I use QTableWidget
from PyQt to display a DataFrame
. I create a QTableWidgetObject
and then populate with QTableWidgetItems
created with DataFrame
values.
Following is the snippet of code that reads a CSV file ,create a DataFrame
, then display in a GUI:
df = read_csv(filename, index_col = 0,header = 0)
self.datatable = QtGui.QTableWidget(parent=self)
self.datatable.setColumnCount(len(df.columns))
self.datatable.setRowCount(len(df.index))
for i in range(len(df.index)):
for j in range(len(df.columns)):
self.datatable.setItem(i,j,QtGui.QTableWidgetItem(str(df.iget_value(i, j))))
Update:
As this answer was quite old, it deserves an update. There are many options available now to view the dataframes in GUI.
- As others have pointed out, Python IDEs such as Spyder
come with dataframe viewers.
- qgrid is
another option for the jupyter notebook widget that renders the dataframes within the notebook.
If someone still wants to code a simple GUI to view the dataframes within Jupyter, following is the complete , minimal example using Pyqt5 .
%gui qt5
from PyQt5.QtWidgets import QWidget,QScrollArea, QTableWidget, QVBoxLayout,QTableWidgetItem
import pandas as pd
win = QWidget()
scroll = QScrollArea()
layout = QVBoxLayout()
table = QTableWidget()
scroll.setWidget(table)
layout.addWidget(table)
win.setLayout(layout)
df = pd.DataFrame({"a" : [4 ,5, 6],"b" : [7, 8, 9],"c" : [10, 11, 12]},index = [1, 2, 3])
table.setColumnCount(len(df.columns))
table.setRowCount(len(df.index))
for i in range(len(df.index)):
for j in range(len(df.columns)):
table.setItem(i,j,QTableWidgetItem(str(df.iloc[i, j])))
win.show()