Analyze CSV Data with Tkinter and Pandas GroupBy and Pivot Tables

CSV data analysis using Pandas GroupBy and Pivot Table through Tkinter

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

Load CSV Data into Pandas Top ↑

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
)

Display Descriptive Statistics Top ↑

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.

Select Columns for Analysis Top ↑

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

Perform GroupBy Analysis Top ↑

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.

Why Not Use groupby().sum() on the Entire DataFrame? Top ↑

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)

Create a Pandas Pivot Table Top ↑

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
]

GroupBy vs Pivot Table Top ↑

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.

Display Analysis Results in Treeview Top ↑

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.

Restore the Source DataFrame Top ↑

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.

Save the Current Analysis Result to SQLite Top ↑

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.

Complete Tkinter Pandas Data Analysis Application Top ↑

Understanding the Current Result Top ↑

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.

Example Analysis Workflow Top ↑

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.

Working with Large CSV Files Top ↑

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:

  • loading only required columns;
  • filtering during import;
  • using suitable Pandas dtypes;
  • performing aggregation in chunks where mathematically appropriate;
  • using a database for larger-than-memory analysis;
  • running long analysis operations outside Tkinter's main thread.

For an example of chunk processing and a responsive Tkinter interface, see the CSV to SQLite Progressbar project.

Continue the Tkinter Pandas Analysis Cluster Top ↑

Work with exported analytics CSV data:

Analytics CSV Data with Tkinter and Pandas

Search and filter DataFrame records:

Search DataFrame in Tkinter

Choose which DataFrame columns should be displayed:

Select DataFrame Columns

Clean CSV data before analysis:

Pandas Data Cleaning GUI

Frequently Asked Questions Top ↑

Q1: What does describe() show in a Pandas DataFrame?

describe() 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.

Q2: What is GroupBy used for?

GroupBy divides records according to one or more grouping fields and calculates values such as sum, mean, count, minimum or maximum for each group.

Q3: What is the difference between GroupBy and a Pivot Table?

GroupBy normally returns grouped records vertically. A Pivot Table can place a second category across columns, which is useful for cross-tabular summaries.

Q4: Why does the application select a numeric value column?

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.

Q5: Does running GroupBy or Pivot Table modify the original DataFrame?

No. The loaded CSV remains in df, while the current analysis output is stored separately in result_df.

Q6: Which DataFrame is saved to SQLite?

The current result_df is saved. This can be the source data, descriptive statistics, a GroupBy result or a Pivot Table result.

Q7: Does Treeview display every row of a very large 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.


Data Cleaning Analytics CSV Project

Tkinter Projects Tkinter Pandas Projects


Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2026   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer