Have an amazing solution built in RAD Studio? Let us know. Looking for discounts? Visit our Special Offers page!
CodeDelphiRAD Studio

Build The Ultimate GUI For Pandas To Perform Complex Data Analysis

Build The Ultimate GUI For Pandas To Perform Complex Data Analysis

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.

Build The Ultimate GUI For Pandas To Perform Complex Data Analysis Am image of two Panda bears
Before you ask no we are <strong>not<strong> suggesting using fluffy black and white Panda bears to perform data analysis 😁

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?

gui for pandas logo
pandas logo Image source pandaspydataorg

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.

gui for pandas trend
pandas popularity over time in the last 10 years compared to the other 4 popular Python libraries Source Google Trends

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:

Alternatively, if you are using the Anaconda Python distribution, you can use the following command to avoid complexities and conflicts between required libraries:

If you use the Anaconda Python distribution, pandas is usually already installed.

 

How do I install Python4Delphi?

Follow the Python4Delphi installation instructions written here. Alternatively, you can follow the simple instructions in Jim McKeeth‘s “Getting Started With Python4Delphi” video:

 

How do I build a Delphi GUI for the pandas data analysis library?

Our project’s user interface structure is as follows:

gui for pandas ui structure
User interface structure for our Pandas4D project

Here is the list of Components used in the Pandas4D demo app:

  1. TPythonEngine
  2. TPythonModule
  3. TPythonType
  4. TPythonVersions
  5. TPythonGUIInputOutput
  6. TForm
  7. TMemo
  8. TOpenDialog
  9. TSaveDialog
  10. TSplitter
  11. TImage
  12. Tpanel
  13. TLabel
  14. TComboBox
  15. TButton
  16. TEdit
  17. TStringGrid

Navigate to the UnitPandas4D.pas, and add the following line to the FormCreate(), to load our basic pandasApp.py script:

gui for pandas load python script
You can seamlessly load the Pandas app python file inside the UnitPandas4Dpas file

And make sure that the pandasApp.py is in the same directory as our Pandas4D.exe or inside your Delphi project folder.

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:

gui for pandas uses lists
Add Jpeg to your uses list

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:

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.

gui for pandas load dataset
This GUI allows you to load a structured dataset that you will work with

This is how the loaded dataset would look like inside the Pandas4D GUI:

gui for pandas load dataset
The loaded Churn Modellingcsv file inside 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.

gui for pandas change python version
You can use this GUI to select any Python version that is installed on your system Then to run the Basic Example by clicking the Execute button

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.

gui for pandas basic example
Pandas4D most basic example

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:

demo01 pandas4d 17 pyscripter 6141009

And vice versa, if you want to visualize the Example 15, do comment the code for Example 14:

demo01 pandas4d 16 pyscripter 6940498

 

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

demo01 pandas4d 02 9336589

2. Examine the dataset’s shape or dimensions

demo01 pandas4d 03 2065139

3. Examine the DataFrame’s column labels

demo01 pandas4d 04 6498308

4. Removing columns

demo01 pandas4d 05 7727084

5. Select particular columns while reading

demo01 pandas4d 06 7963925

6. Reading a portion of the data frame (from the first n number of the rows)

demo01 pandas4d 07 6020870

7. Select rows from the file’s end

demo01 pandas4d 08 1277518

8. Draw a small sample to work

demo01 pandas4d 09 7922845

9. Looking for missing values

demo01 pandas4d 10 2001826

10. Using loc and iloc to add missing values

demo01 pandas4d 11 7339685

11. Fill the missing values

demo01 pandas4d 12 1090088
demo01 pandas4d 13 6740700

12. Drop the missing values

demo01 pandas4d 14 1131465

13. Row selection based on conditions

demo01 pandas4d 15 9905192

14. Using query to describe the conditions

demo01 pandas4d 16 7268799

15. Using scatter plot matrix to visualize bivariate relationships between combinations of variables

demo01 pandas4d 17 4378967

16. Using isin to describe the conditions

demo01 pandas4d 18 9053344

17. Groupby function

demo01 pandas4d 19 7170970

18. Using groupby to apply multiple aggregate functions

demo01 pandas4d 20 7861579

19. Calculate the number of unique values in a column

demo01 pandas4d 21 2755729

20. The rank function

demo01 pandas4d 22 2296170

21. Memory usage function

demo01 pandas4d 23 7420574

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:

gui for pandas final
gui for pandas final

 

What next?

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:

Build The Ultimate GUI For Pandas To Perform Complex Data Analysis - the Python Eco system

And here are the Embarcadero Python Ecosystem licenses:

embarcadero gui for pandas 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.

See What's New in 12.2 Athens See What's New in 12.2 Athens Dev Days of Summer 2-24

Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial   Upgrade Today

   Free Delphi Community Edition   Free C++Builder Community Edition

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

IN THE ARTICLES