Did you know that you can easily use some truly excellent Python libraries to speed up your Delphi app development on Windows? These libraries are extremely simple to use and produce fantastic methods for performing complex and extremely useful data analysis. It’s one of the areas in which Python is particularly strong.
Adding Python to your Delphi code toolbox can improve your app development by introducing new capabilities that enable you to provide innovative and powerful solutions to your app’s users, combining the best of Python’s rich ecosystem of libraries with Delphi’s supreme low-code and unparalleled power for native Windows development.
Are you looking for powerful tools to analyze and manipulate structured data, and build a nice graphical user interface for them?
You can build fast, expressive, insightful, and scalable data analysis tools easily by combining pandas with Delphi. This article will demonstrate how to create a great-looking GUI for performing complex data analysis using Delphi and the Pandas library.
Table of Contents
Where else can I learn about using Pandas to perform complex data analysis?
This article is a remake of the following article which itself is one of the most viewed articles on pythongui.org:
This Pandas4D GUI demo is presented on Coding Boot Camp 2022 powered by LearnDelphi.org and Embarcadero:
Watch this video by Jim McKeeth for a detailed explanation of why you can love both Delphi and Python at the same time and get the best of all worlds!
What is the pandas Library?
Pandas is a Python package that provides fast, flexible, and expressive data structures designed to work easily and intuitively with structured – tabular, multidimensional, potentially heterogeneous – and time-series data.
Pandas is based on NumPy, a Python library for scientific computing and data analysis. NumPy enables developers to gain valuable insights from various datasets. It is also suitable for data manipulation in Excel spreadsheets and SQL tables.
The goal of Pandas is to be the fundamental high-level building block for doing practical, real-world data analysis in Python. Furthermore, it aspires to be the most powerful and flexible open-source data analysis/manipulation tool available in any language. It is already on its way to accomplishing this goal.
The main structure in pandas is a DataFrame, which is a 2D table. It supports a wide range of data formats, including JSON, CSV, SQL, XLSX, and others. Python developers can edit, delete, and manipulate data in the 2D table with just a few lines of code.
How can I obtain the Pandas library?
With pip, you can easily install Pandas:
pip install pandas
Alternatively, if you are using the Anaconda Python distribution, you can use the following command to avoid complexities and conflicts between required libraries:
conda install pandas
If you use the Anaconda Python distribution, pandas is usually already installed.
How do I install Python4Delphi?
How do I build a Delphi GUI for the pandas data analysis library?
Our project’s user interface structure is as follows:
Here is the list of Components used in the Pandas4D demo app:
Memo1.Lines.LoadFromFile(ExtractFilePath(ParamStr(0)) + 'pandasApp.py');
You can change the “pandasApp.py” with any pandas script you want, or you can load your pandas scripts at runtime, by clicking the “Load script…” button like we will show you in the next Demo Sections.
How to analyze data using pandas on the Delphi app?
This article will walk you through the process of integrating Python Pandas into a Delphi-created Windows app. As a bonus, we will show you 20+ cases.
Highly recommended practice:
1. This GUI was created by modifying Python4Delphi Demo34, which allows us to change the Python version in the runtime (this will save you from the seemingly complicated dll issues).
2. Add “Jpeg” to the top of our UnitPandas4D.pas code’s Uses-list. We need to do so because Delphi will not understand the JPG format otherwise. It should work with this change.
Following that, the above modification should look like this:
This way, we can also load JPG images into our TImage to show any data visualization results.
3. In your Environment Variable, add the following paths:
If you’re using the Anaconda Python distribution, create the following paths:
4. Matplotlib must be used outside of the “normal” command-line process to display plotting results. To accomplish this, include the following lines in all of your Python code:
import matplotlib.pyplot as plt
We strongly advise you to name your image output “pandasPlot.jpg” for it to load automatically on your GUI after you click the “Show plot” button.
5. Set MaskFPUExceptions(True); to the UnitPandas4D.pas file to avoid Delphi raising an exception when floating operations result in +/- infinity (e.g. division by zero), which is caused by incompatibility with several Python libraries such as NumPy, SciPy, Pandas, and Matplotlib.
6. The new feature of this latest Python4Delphi GUI demo is we add TStringGrid to show up the dataset we will be working with. First, load the dataset by clicking the “Load .csv file…”, and open the Churn_Modelling.csv file, the data that we will work with.
This is how the loaded dataset would look like inside the Pandas4D GUI:
7. One of the best features of this Pandas4D Demo GUI is that you can choose your preferred Python version, which is interchangeable.
As we’ve already tested this GUI for regular Python and Anaconda Python distribution, this Pandas4D GUI works better for both distributions.
8. Pandas4D most basic example would load the dataset we will work with, and execute the pandasApp.py that will run 20+ amazing pandas data analysis examples.
Next, click “Show plot” if you want to show the data visualization.
9. A little note on how to choose which plot you want to show:Open the pandasApp.py with PyScripter. If you want to visualize Example 14 (plot a histogram for a certain query), do comment on the code for Example 15:
And vice versa, if you want to visualize the Example 15, do comment the code for Example 14:
20+ amazing pandas examples inside the Delphi Windows GUI App
The Python code (pandasApp.py) would do the following data analysis and manipulation operations for you:
1. Reading data from a CSV file into a pandas dataframe
2. Examine the dataset’s shape or dimensions
3. Examine the DataFrame’s column labels
4. Removing columns
5. Select particular columns while reading
6. Reading a portion of the data frame (from the first n number of the rows)
7. Select rows from the file’s end
8. Draw a small sample to work
9. Looking for missing values
10. Using loc and iloc to add missing values
11. Fill the missing values
12. Drop the missing values
13. Row selection based on conditions
14. Using query to describe the conditions
15. Using scatter plot matrix to visualize bivariate relationships between combinations of variables
16. Using isin to describe the conditions
17. Groupby function
18. Using groupby to apply multiple aggregate functions
19. Calculate the number of unique values in a column
20. The rank function
21. Memory usage function
Visit this repository for complete source code.
How the final output of Pandas4D would look like?
The following is the final result that you might see, after running the pandasApp.py inside Delphi GUI and load the plots:
Another best practices we might need in the future:
- Save/record all the text output, instead of only printing it on TMemo.
- Create more dynamic tables using TStringGrid to present the output, instead of TMemo.
- Call the datasets directly from the database.
- Show multiple image outputs.
- Your suggestions!
Get to know more with Embarcadero Python Ecosystem
The following is the current Embarcadero Python Ecosystem:
And here are the Embarcadero Python Ecosystem licenses:
Which you can know more about them from the following webinars by Jim McKeeth:
Congratulations, now you have learned a lot about GUI for pandas: A powerful data analysis and manipulation library, and how you can use Delphi to create a simple yet powerful GUI for it! We have learned 20+ examples and use cases, and now you can explore them to boost your productivity in creating your own data analysis and manipulation apps.
Download a free trial of RAD Studio Delphi today and try out these examples for yourself.