ALL AUTOMATE WORKFLOW EVERYWHERE BUSINESS DATA ANALYSIS ENGLISH ARTICLES MY PROJECTS PYTHON ΧΡΗΜΑΤΟΟΙΚΟΝΟΜΙΚΑ

Interactive Sales Data Analysis with Python and PandasAI

Code Overview: Interactive Sales Data Analysis with Python and PandasAI

# Sample DataFrame
sales_by_country = pd.DataFrame({
    “country”: [“United States”, “United Kingdom”, “France”, “Germany”, “Italy”, “Spain”, “Canada”, “Australia”, “Japan”, “China”, “Malta”],
    “sales”: [5000, 3200, 2900, 4100, 2300, 2100, 2500, 2600, 4500, 7000, 2900]
})

This Python code creates an interactive desktop application for analyzing sales data across various countries. The application, built with tkinter, allows users to select from predefined analytical questions and view dynamic responses based on data processed with pandas and PandasAI. Here’s a breakdown of the functionality:

  1. Data and API Setup
  • A sample DataFrame, sales_by_country, stores country names and their respective sales figures.
  • An API key is securely loaded from a config.yaml file to enable communication with PandasAI, an AI-powered data analysis tool, which provides language-based data interaction.
  1. User Interface with tkinter
  • Main Window: A tkinter window, titled “PandasAI Question Selector,” is created to house all interactive elements.
  • Title and Dropdown: The interface includes a title label and a dropdown list (ttk.Combobox) that allows users to select questions.
  • Questions List: Users can choose from questions like “Which are the top 5 countries by sales?” and “Which countries contribute 80% of the sales?”.

Sales by Country

  1. Data Display with tkinter Frames
  • Results Table (table_frame): Displays responses in a table format. For instance, selecting “Which country has the highest sales?” displays the country name and sales figure for the top-performing country.
  • Summary Frame (summary_frame): Shows summary details below the results table for specific questions, such as the cumulative sales percentage of countries contributing to 80% or more of total sales.
  1. Functions for Data Analysis
  • Data Clearing (clear_table()): Clears any previous table or summary information each time a new question is selected.
  • 80% Sales Threshold (countries_representing_80_percent()): Calculates the smallest list of countries that make up 80% or more of total sales. The function sorts countries by sales, calculates cumulative sales, and returns the relevant countries along with their sales data.
  1. Answer Display Logic (get_response())
  • Dynamic Response Handling: Depending on the selected question, get_response() uses pandas functions (e.g., .sum(), .sort_values(), and .iterrows()) to analyze the data and display the result in a clear, structured format. For example:
    • If “What is the total sales?” is selected, the total sales value is calculated and displayed, along with an alphabetized list of countries.
    • If “Show the original DataFrame” is selected, the entire DataFrame is displayed in a scrollable table.
  • Displaying Cumulative Sales Information: For questions like “Which countries contribute 80% of the sales?”, the application displays relevant data in the results table and populates the summary_frame with additional information, such as the total sales and percentage represented by those countries.
  1. User Experience
  • Answer Button: After selecting a question, users click the “Get Answer” button, which triggers get_response() to update the display with the latest results.
  • Clear and Reset: Every new question selection clears previous results, allowing users to easily explore different analyses without overlapping data.

Code Structure and Technologies

  • Python Libraries: pandas for data manipulation, yaml for secure API key handling, and tkinter for creating a responsive graphical interface.
  • PandasAI: Used to handle queries with natural language processing (NLP), allowing the application to dynamically interpret questions and retrieve accurate responses based on the data.

This code provides a robust and user-friendly way to explore sales data, using a combination of Python’s data analysis capabilities and AI-driven interactivity. With a simple dropdown and a clean layout, it’s an excellent example of combining data science and desktop application development for interactive analytics.

Let me know by email if you’d like any additional details!

Python code (Part of it with dark background)

Talk with your dataClick the links below to see the full code:

Python code with dark background

Python code with white background

PandasAI API keys for LLM Usage: https://www.pandabi.ai/admin/api-keys

PandasAI

Views: 8

Comments are closed.

Pin It