In this PyQt5 article we want to learn How to Integrate PyQt5 with Pandas, so PyQt5 is powerful GUI toolkit for Python, and Pandas is popular library for data analysis in Python. Integrating PyQt5 with Pandas allows you to create interactive and dynamic data driven applications.
First of all you need to install PyQt5 and Pandas
1 |
pip install PyQt5 pandas |
This is the complete code for this article
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 |
import sys from PyQt5.QtWidgets import QApplication, QMainWindow, QTableView from PyQt5.QtGui import QStandardItemModel, QStandardItem import pandas as pd class MainWindow(QMainWindow): def __init__(self): super().__init__() self.table = QTableView(self) self.setCentralWidget(self.table) self.setWindowTitle("Geekscoders.com") # Create sample data data = {'Name': ['John', 'Geekscoders', 'Bob', 'Doe'], 'Age': [30, 25, 40, 28], 'Country': ['USA', 'Canada', 'UK', 'Germany']} df = pd.DataFrame(data) # Create QStandardItemModel model = QStandardItemModel(df.shape[0], df.shape[1]) model.setHorizontalHeaderLabels(df.columns) # Populate QStandardItemModel with data for row in range(df.shape[0]): for column in range(df.shape[1]): item = QStandardItem(str(df.iloc[row, column])) model.setItem(row, column, item) # Set model for QTableView self.table.setModel(model) if __name__ == '__main__': app = QApplication(sys.argv) window = MainWindow() window.show() sys.exit(app.exec_()) |
In the above code first we have imported our require libraries and modules from PyQt5. for example we have imported specific classes from PyQt5.QtWidgets and PyQt5.QtGui libraries, which are used to build GUI. specifically QApplication class that creates the main application object, QMainWindow class creates main application window and QTableView class provides a view onto a table model.
After that we have defined custom class called MainWindow that extends from QMainWindow. in init method of the MainWindow class we have set up QTableView widget as the central widget of the main window. and then we creates sample data DataFrame and QStandardItemModel, which is specialized model designed to work with the QTableView widget. model is then populated with data from the DataFrame, and finally model is set for QTableView widget.
Run the code and this will be the result
Learn More on PyQt5
- How to Deploy PyQt5 Applications
- How to Integrate PyQt5 with OpenCV
- How to Use Stylesheets in PyQt5
- Learn PyQt5 Event Handling with Signals and Slots
- PyQt5 Tutorial: Creating Menus and Toolbars
- How to Open & Save Files in Python PyQt5
- PyQt5 Designer Tutorial: Complete Guide to Qt Designer
- PyQt5 vs Tkinter: Which GUI Library is Right for You
- How to Build Web Applications in PyQt5
- How to Integrate Python Flask with PyQt5