Create a CSV Data Analysis and SQLite Saver Tool with Tkinter and Pandas


Data Analysis by using CSV data and storing in SQLite table
Use the read_csv() method to create a Pandas Dataframe.

1. Importing Required Libraries

import tkinter as tk
from tkinter import filedialog, messagebox, ttk, simpledialog
import pandas as pd
from sqlalchemy import create_engine
  • Tkinter is used to create the GUI for file browsing and user input.
  • Pandas handles data operations like loading, grouping, and pivoting CSV files.
  • SQLAlchemy helps connect and save data to an SQLite database.

2. Defining Global Variables

global df, engine  
df = None  # Pandas DataFrame to hold CSV data
engine = None  # SQLite database engine

3. Loading CSV Files

def load_csv():
    """ Load CSV file and display first few rows in the text area """
    global df
    file_path = filedialog.askopenfilename(
        filetypes=[("CSV files", "*.csv")]
    )
    if file_path:
        try:
            df = pd.read_csv(file_path)
            text_area.delete("1.0", tk.END)
            text_area.insert(tk.END, f"CSV loaded successfully!\n\n{df.head()}")
        except Exception as e:
            messagebox.showerror("Error", f"Failed to load CSV: {e}")
  • Allows users to select a CSV file using a file dialog.
  • Loads the data into a Pandas DataFrame and displays the first few rows in a text area.
  • Handles errors if the file cannot be loaded.

4. Displaying Descriptive Statistics

def show_describe():
    """ Display descriptive statistics of the DataFrame """
    if df is None:
        messagebox.showwarning("Warning", "No data loaded. Load a CSV file first.")
        return
    description = df.describe()
    text_area.delete("1.0", tk.END)
    text_area.insert(tk.END, f"Descriptive Statistics:\n\n{description}")
  • Displays summary statistics like count, mean, min, and max values for numerical columns.
  • Ensures that data is loaded before running this function.

5. Performing Group By Operations

def group_by():
    """ Group data by a column entered by the user and calculate the sum """
    global df
    if df is None:
        messagebox.showwarning("Warning", "No data loaded. Load a CSV file first.")
        return
    group_col = simpledialog.askstring("Input", 
                        f"Enter the column to group by:\n{list(df.columns)}")
    if group_col in df.columns:
        grouped = df.groupby(group_col).sum()
        text_area.delete("1.0", tk.END)
        text_area.insert(tk.END, f"Group By '{group_col}':\n\n{grouped}")
  • Prompts the user to enter a column name to group by.
  • Performs a "Group By" operation and calculates the sum for each group.

6. Creating Pivot Tables

def pivot_table():
    """ Generate a Pivot Table based on user-specified columns """
    if df is None:
        messagebox.showwarning("Warning", "No data loaded. Load a CSV file first.")
        return
    index_col = simpledialog.askstring("Input", "Enter index column:")
    value_col = simpledialog.askstring("Input", "Enter value column:")
    if index_col in df.columns and value_col in df.columns:
        pivot = df.pivot_table(index=index_col, values=value_col, aggfunc="sum")
        text_area.delete("1.0", tk.END)
        text_area.insert(tk.END, f"Pivot Table:\n{pivot}")
  • Creates a Pivot Table by aggregating values using user-defined index and value columns.

7. Saving Data to SQLite Database

def save_to_sqlite():
    """ Save the loaded DataFrame to an SQLite database """
    global df, engine
    if df is None:
        messagebox.showwarning("Warning", "No data loaded. Load a CSV file first.")
        return
    file_path = filedialog.asksaveasfilename(defaultextension=".db")
    if file_path:
        engine = create_engine(f"sqlite:///{file_path}")
        table_name = simpledialog.askstring("Input", "Enter table name:")
        df.to_sql(table_name, engine, index=False, if_exists="replace")
  • Saves the DataFrame into an SQLite database with a user-specified table name.


import tkinter as tk
from tkinter import filedialog, messagebox, ttk, simpledialog
import pandas as pd
from sqlalchemy import create_engine

# Global variables to store the DataFrame and SQLite engine
df = None  # Pandas DataFrame to hold the CSV data
engine = None  # SQLite Database engine for saving data

# Function to load a CSV file into a Pandas DataFrame
def load_csv():
    """
    Opens a file dialog to load a CSV file.
    Displays the first few rows of the loaded data in the text area.
    """
    global df
    # Open a file dialog to select the CSV file
    file_path = filedialog.askopenfilename(
        filetypes=[("CSV files", "*.csv"), ("All files", "*.*")]
    )
    if file_path:
        try:
            # Load the CSV file into a Pandas DataFrame
            df = pd.read_csv(file_path)
            # Clear the text area and display the first few rows of data
            text_area.delete("1.0", tk.END)
            text_area.insert(tk.END, f"CSV loaded successfully!\n\n{df.head()}")
            messagebox.showinfo("Success", "CSV file loaded successfully!")
        except Exception as e:
            # Display an error message if the CSV file fails to load
            messagebox.showerror("Error", f"Failed to load CSV: {e}")

# Function to show descriptive statistics for the loaded data
def show_describe():
    """
    Displays descriptive statistics for the loaded DataFrame.
    Shows statistics like mean, min, max, and count.
    """
    global df
    if df is None:
        # Check if the data is loaded
        messagebox.showwarning("Warning", "No data loaded. Load a CSV file first.")
        return
    # Compute descriptive statistics and display them
    description = df.describe()
    text_area.delete("1.0", tk.END)
    text_area.insert(tk.END, f"Descriptive Statistics:\n\n{description}")

# Function to perform a "Group By" operation
def group_by():
    """
    Groups the data by a user-specified column and calculates the sum.
    Prompts the user to enter a column name.
    """
    global df
    if df is None:
        messagebox.showwarning("Warning", "No data loaded. Load a CSV file first.")
        return
    # Get column names for reference
    columns = list(df.columns)
    # Ask the user to enter the column name to group by
    group_col = simpledialog.askstring("Input", f"Enter the column name to group by:\n{columns}")
    if group_col and group_col in df.columns:
        # Group by the specified column and calculate the sum
        grouped = df.groupby(group_col).sum()
        text_area.delete("1.0", tk.END)
        text_area.insert(tk.END, f"Group By '{group_col}':\n\n{grouped}")
    else:
        messagebox.showerror("Error", "Invalid column name!")

# Function to create a Pivot Table
def pivot_table():
    """
    Creates a Pivot Table based on user-specified index and value columns.
    Prompts the user to enter column names.
    """
    global df
    if df is None:
        messagebox.showwarning("Warning", "No data loaded. Load a CSV file first.")
        return
    # Get column names for reference
    columns = list(df.columns)
    # Ask user for the index and value columns
    index_col = simpledialog.askstring("Input", f"Enter the index column for Pivot Table:\n{columns}")
    value_col = simpledialog.askstring("Input", f"Enter the value column for Pivot Table:\n{columns}")
    if index_col in df.columns and value_col in df.columns:
        # Generate the Pivot Table
        pivot = df.pivot_table(index=index_col, values=value_col, aggfunc="sum")
        text_area.delete("1.0", tk.END)
        text_area.insert(tk.END, f"Pivot Table (Index: {index_col}, Value: {value_col}):\n\n{pivot}")
    else:
        messagebox.showerror("Error", "Invalid column names!")

# Function to save the current DataFrame to an SQLite database
def save_to_sqlite():
    """
    Saves the loaded DataFrame to an SQLite database.
    Prompts the user to choose a file location and enter a table name.
    """
    global df, engine
    if df is None:
        messagebox.showwarning("Warning", "No data loaded. Load a CSV file first.")
        return
    # Open a file dialog to choose where to save the SQLite database
    file_path = filedialog.asksaveasfilename(
        defaultextension=".db", filetypes=[("SQLite Database", "*.db")]
    )
    if file_path:
        try:
            # Create an SQLite database engine
            engine = create_engine(f"sqlite:///{file_path}")
            # Prompt user to provide a table name
            table_name = simpledialog.askstring("Input", "Enter the table name:")
            if table_name:
                # Save the DataFrame to the SQLite database
                df.to_sql(table_name, engine, if_exists="replace", index=False)
                messagebox.showinfo("Success", f"Data saved to SQLite database as '{table_name}' table.")
            else:
                messagebox.showerror("Error", "Table name cannot be empty.")
        except Exception as e:
            # Handle errors during the save operation
            messagebox.showerror("Error", f"Failed to save data to SQLite: {e}")

# GUI Setup
root = tk.Tk()
root.title("CSV Data Analysis and SQLite Saver")  # Title of the application
root.geometry("800x600")  # Set window size

# Frame for buttons at the top
frame = tk.Frame(root)
frame.pack(pady=10)

# Buttons for various operations
btn_load = tk.Button(frame, text="Load CSV", command=load_csv, width=15, bg="lightblue")
btn_load.grid(row=0, column=0, padx=5)

btn_describe = tk.Button(frame, text="Describe Data", command=show_describe, width=15, bg="lightgreen")
btn_describe.grid(row=0, column=1, padx=5)

btn_groupby = tk.Button(frame, text="Group By", command=group_by, width=15, bg="lightyellow")
btn_groupby.grid(row=0, column=2, padx=5)

btn_pivot = tk.Button(frame, text="Pivot Table", command=pivot_table, width=15, bg="orange")
btn_pivot.grid(row=0, column=3, padx=5)

btn_save_sqlite = tk.Button(frame, text="Save to SQLite", command=save_to_sqlite, width=15, bg="lightcoral")
btn_save_sqlite.grid(row=0, column=4, padx=5)

# Text area to display output or results
text_area = tk.Text(root, wrap="word", font=("Arial", 10))
text_area.pack(expand=True, fill="both", padx=10, pady=10)

# Start the main event loop for the GUI
root.mainloop()

Conclusion

This project combines Tkinter, Pandas, and SQLAlchemy to build a fully functional SQLite database viewer with sorting and export features.




Part I : Searching DataFrame and displaying result in Treeview
Part III : Selection Columns of DataFrame using CheckButtons
Browsing directory and displaying file details with sorting using Treeview
Projects in Tkinter
Create Pandas DataFrame by reading Google Analytics csv file from Tkinter GUI Search DataFrame by user inputs through Tkinter.
Tkinter Projects Projects Tkinter using Pandas DataFrame

Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    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 FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer