In this PyQt6 article we want to learn How to Integrate PyQt6 with Pandas, so PyQt6 is powerful GUI framework for creating desktop applications, and Pandas is popular data manipulation library for Python. integrating these two can be incredibly useful, as it allows you to build desktop applications that can handle and display large amounts of data. in this article we want to talk about integrating PyQt6 with Pandas.
How to Integrate PyQt6 with Pandas
First of all we need to install required libraries, and we can use pip for the installation
1 2 |
pip install PyQt6 pip install 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 |
import sys import pandas as pd from PyQt6.QtWidgets import QMainWindow, QApplication, QTableWidget, QTableWidgetItem class MyApp(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle('My PyQt6 App') self.setGeometry(100, 100, 800, 600) # Create a Pandas DataFrame data = {'Name': ['John', 'Mike', 'Sarah'], 'Age': [32, 24, 28], 'Occupation': ['Engineer', 'Teacher', 'Doctor']} df = pd.DataFrame(data) # Add a table to the main window self.table = QTableWidget() self.setCentralWidget(self.table) # Populate the table with data from the DataFrame self.table.setColumnCount(len(df.columns)) self.table.setRowCount(len(df.index)) for i in range(len(df.index)): for j in range(len(df.columns)): self.table.setItem(i, j, QTableWidgetItem(str(df.iloc[i, j]))) if __name__ == '__main__': app = QApplication(sys.argv) myapp = MyApp() myapp.show() sys.exit(app.exec()) |
So in the above code we have our Pandas DataFrame. for the purposes of this tutorial we want to create simple DataFrame with three columns.
1 2 3 4 |
data = {'Name': ['Geekscoders', 'Mike', 'Sarah'], 'Age': [32, 24, 28], 'Occupation': ['Engineer', 'Teacher', 'Doctor']} df = pd.DataFrame(data) |
Now that we have our DataFrame we can start creating our PyQt6 application. we want to start by creating a class that extends from QMainWindow:
1 2 3 4 5 |
class MyWindow(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle('Geekscoders.com') self.setGeometry(100, 100, 800, 600) |
Next we want to add table to our PyQt6 application that displays our Pandas DataFrame. To do this, we want to use QTableWidget class:
1 2 |
self.table = QTableWidget() self.setCentralWidget(self.table) |
Run the complete code and this will be the result
Learn More on Python GUI
- Qt Designer Tool in PyQt6
- How to Use Stylesheets in Python PyQt6
- Event Handling in Python and PyQt6
- Responsive Applications with PyQt6 Multithreading
- How to Deploy PyQt6 Applications
- How to Build Web Applications with PyQt6
- How to Create ComboBox in PySide6
- How to Create CheckBox in Python PySide6
- Responsive Applications with PyQt6 Multithreading
- Event Handling in Python and PyQt6
- How to Use Stylesheets in Python PyQt6