
This project uses Tkinter to build an interactive interface for basic Pandas data analysis. The user selects a CSV file, creates a DataFrame with read_csv(), generates descriptive statistics, performs GroupBy analysis and creates Pivot Tables.
The current source DataFrame or analysis result is displayed in a Treeview. The current result can then be stored in an SQLite table.
CSV file
|
v
Pandas DataFrame
|
+--> describe()
|
+--> groupby()
|
+--> pivot_table()
|
v
Analysis result
|
+--> Treeview preview
|
+--> SQLite table
Show Table of Contents
The Tkinter file browser lets the user select a CSV file:
file_path=filedialog.askopenfilename(
title='Select CSV file',
filetypes=[
('CSV files','*.csv'),
('All files','*.*')
]
)
Create the DataFrame:
df=pd.read_csv(file_path)
After loading, the application reads the available column names and identifies numeric columns that can be used as values for calculations.
columns=list(df.columns)
numeric_columns=list(
df.select_dtypes(
include='number'
).columns
)
Pandas describe() summarizes the DataFrame.
Using:
df.describe()
primarily summarizes numeric columns.
For this analysis viewer, we use:
result=df.describe(
include='all'
).transpose().reset_index()
result=result.rename(
columns={
'index':'column'
}
)
Transposing the output places one original DataFrame column on each result row.
Depending on the source column type, the statistics can include:
count
unique
top
freq
mean
std
min
25%
50%
75%
max
Not every statistic applies to every data type, so some cells can remain blank.
The original program asked users to type DataFrame column names into dialog boxes. This can lead to spelling errors.
The revised interface uses read-only Comboboxes populated directly from df.columns.
For GroupBy we select:
Group column
Value column
Aggregation
For a Pivot Table we select:
Row field
Column field
Value field
Aggregation
The supported aggregation operations are:
sum
mean
count
min
max
Pandas groupby() divides records into groups based on one column and calculates a summary for another column.
For example, if the DataFrame contains:
class | mark
Four | 75
Three | 85
Three | 55
Four | 60
we can calculate the average mark for each class.
result=df.groupby(
group_column,
dropna=False
)[value_column].agg(
aggregation
).reset_index()
For:
group_column = 'class'
value_column = 'mark'
aggregation = 'mean'
the result represents one row per class with its calculated mean mark.
The older example used:
df.groupby(group_col).sum()
A DataFrame can contain IDs, text, dates and numeric measures. It is clearer to explicitly select the value column that should be aggregated.
df.groupby(group_column)[value_column].agg(aggregation)
A Pivot Table can summarize one numeric value across two categorical dimensions.
Suppose the CSV contains:
class | gender | mark
Four | female | 75
Four | male | 60
Three | female | 85
Three | male | 55
Choose:
Rows = class
Columns = gender
Values = mark
Aggregation = mean
Create the Pivot Table:
result=pd.pivot_table(
df,
index=row_column,
columns=pivot_column,
values=value_column,
aggfunc=aggregation
)
Reset the index before displaying the result:
result=result.reset_index()
The Pivot Table columns are also converted to display-friendly text:
result.columns=[
str(column)
for column in result.columns
]
Both operations summarize data, but they are useful in different ways.
GroupBy produces a grouped result such as:
class | mean_mark
Four | 67.5
Three | 70.0
Pivot Table can spread a second category across columns:
class | female | male
Four | 75 | 60
Three | 85 | 55
This makes Pivot Tables useful for cross-tab style analysis.
The Treeview is created once and reused for all operations.
Safe internal column identifiers are created:
column_ids=[
f'c{i}'
for i in range(
len(data.columns)
)
]
The real DataFrame column names remain visible as headings.
Only a limited number of rows are inserted into the GUI:
preview=data.head(
MAX_DISPLAY_ROWS
)
The complete analysis result remains stored in result_df. Limiting Treeview rows prevents very large DataFrames from creating thousands of GUI items unnecessarily.
Analysis operations do not modify the loaded source DataFrame.
df
The current analysis output is stored separately:
result_df
The Show Source Data Button sets:
result_df=df.copy()
and displays the original loaded records again.
The old application always saved the original df, even after the user generated a GroupBy or Pivot Table.
The revised application saves:
result_df
Therefore the workflow can be:
CSV
|
v
GroupBy
|
v
Grouped DataFrame
|
v
Save Current Result
|
v
SQLite table
or:
CSV
|
v
Pivot Table
|
v
Pivot DataFrame
|
v
SQLite table
The table name is validated and an existing SQLite table is not replaced without confirmation.
The application maintains two DataFrames:
df
result_df
df always refers to the CSV data loaded from disk.
result_df refers to whatever the user is currently viewing:
Source DataFrame
Descriptive Statistics
GroupBy result
Pivot Table
This separation means an analysis does not overwrite the underlying source data.
Assume the CSV contains student data:
id | name | class | gender | mark
To find average marks by class:
Group / Pivot rows = class
Value = mark
Aggregation = mean
Run GroupBy
To compare average marks by class and gender:
Group / Pivot rows = class
Pivot columns = gender
Value = mark
Aggregation = mean
Run Pivot Table
This illustrates the progression from raw records to a summarized DataFrame.
This tutorial loads the complete CSV into memory because GroupBy, descriptive statistics and Pivot Tables operate on the DataFrame as a whole.
For very large datasets, consider:
For an example of chunk processing and a responsive Tkinter interface, see the CSV to SQLite Progressbar project.
Work with exported analytics CSV data:
Analytics CSV Data with Tkinter and PandasSearch and filter DataFrame records:
Search DataFrame in TkinterChoose which DataFrame columns should be displayed:
Select DataFrame ColumnsClean CSV data before analysis:
Pandas Data Cleaning GUIdescribe() generates descriptive statistics. Numeric columns can include count, mean, standard deviation, minimum, quartiles and maximum, while other data types can provide statistics such as count, unique values, top value and frequency.
GroupBy divides records according to one or more grouping fields and calculates values such as sum, mean, count, minimum or maximum for each group.
GroupBy normally returns grouped records vertically. A Pivot Table can place a second category across columns, which is useful for cross-tabular summaries.
Operations such as sum and mean require values that can be numerically aggregated. Restricting the value selector avoids accidentally applying those calculations to unrelated text columns.
No. The loaded CSV remains in df, while the current analysis output is stored separately in result_df.
The current result_df is saved. This can be the source data, descriptive statistics, a GroupBy result or a Pivot Table result.
The example displays up to 500 rows to keep the GUI manageable. The complete DataFrame remains available in memory and is used when saving the result to SQLite.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.