Analytics Vidhya
Published in

Analytics Vidhya

How to make GUI in python for data analysis

I wrote a medium article on how I made a data analysis pipeline using Excel VBA. It works well for most of my needs. But Excel is not a good tool to handle large data. And data that we need to handle keeps on increasing.

So I wrote a python program to handle my data analysis work. In this article I will outline the steps I had taken to create the python program for data analysis task.

We have following data in data sheet of excel .

We have a list of users across different groups and locations. Columns Revenue and Expense contains revenue and expense to the company from each user.

We want to create following report

For each group, we want consolidated revenues and number of users. In the previous article I created this table using excel. Now I will show how to create this table in Python.

I am using Python 3.6.9 and Pandas 0.22 for data analysis. You can install python and pip by following instructions from this website. Afterwards you can install any python library using pip. Just run the following command on cmd.

pip install library_name

Afterwards first we import the pandas library

import pandas

In next few steps I will show the code to make the GUI.

1 . Reading file

We can read the data file using the below code

data = pandas.read_excel(‘data.xls’)

This loads the data in Dataframe object of pandas. You can read more about Dataframe API of pandas here. Dataframe is similar to Excel table in structure. Data is stored in Dataframe object in a rows and columns structure similar to Excel. You can access a particular data in Dataframe by referring to its row number and column number like Excel. In addition you can access data by referring to its row and column names.

2 . Creating table

We can create the required table by the following code

groups = data.groupby('Group')
table = groups.agg({'Revenue':sum, 'Group': len})

Groupby function of pandas divides the data into different groups on basis of data in specified column. You can read more about Groupby API here.

We apply agg function to groups created by Groupby. We have specified sum function for aggregation of ‘Revenue’ column in data and len function for aggregation of ‘Group’ column. End result is table which contains sum of revenue and number of users for each group.

3. Saving Table

Now we can save the output table using the following code

table.to_csv('table.csv')

This will save output table as ‘table.csv’ file in working directory of the code.

4. Jupyter Notebook

All of the above code we can execute in Jupyter notebook. It is my go to tool for data analysis tasks. You can interactively execute your code in jupyter notebook. Not only you can process your data, but you can also view and plot your data inside the notebook.

If the person with whom you want to share your code, is also a programmer then you can just share your code with them.

5 Tkinter GUI

Otherwise you want to provide GUI for non programmers to interact with your code. For creating a GUI in python, Tkinter library is one of the best libraries.

So I made a graphical user interface using Tkinter for above code as below.

When you run the above code, a GUI pops up as shown below

A GUI is like code running in an infinite loop. In the above code, lines between line 18 and 25 is run once. Then python program lies in wait for events like click on buttons, menus, mouse hover etc. In our code the button named ‘Generate Table’ is linked to analysis function. When an user clicks on ‘Generate Table’ button then analysis function is called.

Analysis function asks for data file through windows prompt. Once you select the file , the data is processed. And the function ask for save name and saves the file with specified name.

6. Compiling with pyinstaller

Then to distribute the program you need to create an executable file using pyinstaller library. First you have to install the pyinstaller library. Then you need to save the code in step 5 as ‘filename.pyw’. Note the .pyw extension. If you are using tkinter in your python code then you need to save it .pyw extension before running it through pyinstaller.

Then open command prompt and go into the directory in which your code is present and run the following command in cmd

pyinstaller --noconsole --onefile filename.pyw

This will save ‘filename.exe’ in ‘dist’ subfolder in current directory. You can distribute the executable for use by other persons in your organisation.

Summary

It is daunting to make the jump from Excel to python language. Not only you have to learn python programming language but you have also to learn many libraries like pandas and numpy.

However once you have mastered the libraries and language. Python provides much greater power for data analysis.

In this article I only wanted to give basic introduction to each of the libraries required to make a GUI for data analysis. In the next few articles I will write about each of them in detail.

Next I will be writing how to do common excel tasks using Pandas library.

--

--

--

Analytics Vidhya is a community of Analytics and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com

Recommended from Medium

Portfolio-Scale Machine Learning at Zynga

Data.World Quick Start

Facies analysis on seismic data

JSON and textual data in Python

Exploring DataScienceSalaries Dataset

ARIMA Modeling

Data Preparations

A lot of these questions are just different little derivatives to help you deal with your

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Rohit Raj

Rohit Raj

Studied at IIT Madras and IIM Indore. Love Data Science

More from Medium

Python Dashboard using Plotly dash: Example 2

Cohort Analysis with python

PULLING FINANCIAL DATASET USING YAHOO FINANCE API IN PYTHON

Using style.applymap() as an Seaborn Heatmap alternative