Part 2: Data Analysis with powerful Python

Analyzing and visualizing data from a SQLite database in Python can be a powerful way to gain insights and present your findings. In this blog, I will walk you through the steps to retrieve data from a SQLite database file named gold.db and display it in the form of a chart using Python. We'll use some essential tools and libraries for this task.

Tools you will need !

Before we start, make sure you have the following tools and libraries installed:

  1. Python: Python is the core programming language for this task. You can download it from python.org.

  2. SQLite Database Browser: You can use a tool like DB Browser for SQLite to explore the database and its structure. This is optional but can be helpful for understanding the database schema.

  3. Jupyter Notebook (optional): Jupyter Notebook is an interactive environment that makes data analysis and visualization easier. You can install it using pip: pip install jupyter.

  4. Python Libraries:

    • sqlite3: This is a built-in library for Python that allows you to work with SQLite databases.

    • pandas: A popular data manipulation library for Python.

    • matplotlib: A widely used library for creating charts and visualizations.

You can install the required libraries using pip:

pip install sqlite3 pandas matplotlib

Steps to analyze and visualize data from SQLite database

Now, let's dive into the steps to analyze and display data from the gold.db database:

Step 1: Connect to the database

First, you need to connect to the SQLite database using the sqlite3 library. Here's how you can do it:

python
import sqlite3
# Connect to the database
conn = sqlite3.connect("gold.db")

Step 2: Query the database to retrieve the required data

query = """
    SELECT
        strftime('%Y-%m', o.OrderDate) AS Month,
        p.MetalType AS ProductType,
        SUM(od.Quantity * od.PriceAtTimeOfPurchase) AS TotalSales
    FROM Orders o
    JOIN OrderDetails od ON o.OrderID = od.OrderID
    JOIN Product p ON od.ProductID = p.ProductID
    GROUP BY Month, ProductType
    ORDER BY Month
"""

Execute the query and fetch the data into a Pandas dataframe:

import pandas as pd 
df = pd.read_sql_query(query, db_connection)

Step 3: Pivot the data for plotting

pivot_df = df.pivot(index='Month', columns='ProductType', values='TotalSales')
pivot_df.fillna(0, inplace=True)

Step 4: Create a chart

Now, it's time to create a chart using the matplotlib library. Let's say you want to create a line chart to visualize the type product prices over time:

import matplotlib.pyplot as pltplt.
# Create a line chartfigure(figsize=(12, 6))
for product_type in pivot_df.columns:
    plt.plot(pivot_df.index, pivot_df[product_type], marker='o', linestyle='-', label=product_type)
    
plt.xlabel('sales Month and year')
plt.ylabel('Total Sales')
plt.title('Total Product Sales by Month')
plt.legend(loc='upper left')
plt.grid(True)
# Rotate x-axis labels for better readability
plt.xticks(rotation=90)
plt.show()

Step 5: Save or display the chart

You can choose to save the chart to a file using plt.savefig("gold_prices_chart.png") or display it within a Jupyter Notebook if you're using one.

That's it! You have successfully analyzed data from the SQLite database and displayed it as a chart in Python. You can adapt these steps to your specific data and charting requirements. Remember to close the database connection when you're done:

# Close the database connection
db_connection.close()

In this blog post, we've covered the essential steps and tools to analyze and visualize data from a SQLite database in Python. Data analysis and visualization are crucial skills for various fields, and Python makes it accessible and powerful for these tasks.

Blog 11/24/23

Part 3: How to Analyze a Database File with GPT-3.5

In this blog, we'll explore the proper usage of data analysis with ChatGPT and how you can analyze and visualize data from a SQLite database to help you make the most of your data.

Blog 1/29/24

Database Analysis Report

This report comprehensively analyzes the auto parts sales database. The primary focus is understanding sales trends, identifying high-performing products, Analyzing the most profitable products for the upcoming quarter, and evaluating inventory management efficiency.

Blog 11/27/23

Part 4: Save Time and Analyze the Database File

ChatGPT-4 enables you to analyze database contents with just two simple steps (copy and paste), facilitating well-informed decision-making.

Blog 3/11/21

Introduction to Web Programming in F# with Giraffe – Part 2

In this series we are investigating web programming with Giraffe and the Giraffe View Engine plus a few other useful F# libraries.

Blog 3/10/21

Introduction to Web Programming in F# with Giraffe – Part 1

In this series we are investigating web programming with Giraffe and the Giraffe View Engine plus a few other useful F# libraries.

Blog 3/12/21

Introduction to Web Programming in F# with Giraffe – Part 3

In this series we are investigating web programming with Giraffe and the Giraffe View Engine plus a few other useful F# libraries.

Navigationsbild zu Business Intelligence
Service

Business Intelligence

Business Intelligence (BI) is a technology-driven process for analyzing data and presenting usable information. On this basis, sound decisions can be made.

Technologie 1/7/22

Advice around Mendix

Develop your solutions quickly and independently in low-code with the leading technology vendor. Use the Mendix toolkit and model your applications with visual elements.

Kompetenz 5/14/24

Graph Technology

We help you harness the power of graphs to transform your business. Our expertise spans from graph database modelling and graph data science to generative AI.

Headerbild Talend Data Integration
Technologie

Talend Data Integration

Talend Data Integration offers a highly scalable architecture for almost any application and any data source - with well over 900 connectors from cloud solutions like Salesforce to classic on-premises systems.

Blog 12/7/22

State of Fast Feedback in Data Science Projects

DSML projects can be quite different from the software projects: a lot of R&D in a rapidly evolving landscape, working with data, distributions and probabilities instead of code. However, there is one thing in common: iterative development process matters a lot.

Headerbild zu Data Governance Consulting
Service

Data Governance

Data Governance describes all processes that aim to ensure the traceability, quality and protection of data. The need for documentation and traceability increases exponentially as more and more data from different sources is used for decision-making and as a result of the technical possibilities of integration in Data Warehouses or Data Lakes.

Headerbild zu IBM Spectrum Protect
Technologie 5/20/21

IBM Spectrum Protect

Spectrum Protect is the leading data protection solution from the American industry leader IBM. As TIMETOACT, we take a holistic view of cyber security and focus on building resilient IT and OT.

Felss Logo
Referenz

Quality scoring with predictive analytics models

Felss Systems GmbH relies on a specially developed predictive analytics method from X-INTEGRATE. With predictive scoring and automation, the efficiency of industrial machinery is significantly increased.

Headerbild zu Big Data, Data Lake und Data Warehouse
Service

Big Data, Data Lake & Data Warehousing

For the optimal solution – with special consideration of the business requirements – we combine different functionalities.

Technologie 10/25/24

Advice around Mendix

Develop your solutions quickly and independently in low-code with the leading technology vendor. Use the Mendix toolkit and model your applications with visual elements.

Blog 8/11/22

Part 1: TIMETOACT Logistics Hackathon - Behind the Scenes

A look behind the scenes of our Hackathon on Sustainable Logistic Simulation in May 2022. This was a hybrid event, running on-site in Vienna and remotely. Participants from 12 countries developed smart agents to control cargo delivery truck fleets in a simulated Europe.

Headerbild zu Webserver mit Open Source
Technologie 11/12/20

Web server with Open Source

Web servers provide their application with the gateway to the world: this is where requests for data for a complex web app and resources for a website go in and out.

Headerbild zu Cloud Pak for Data – Test-Drive
Technologie

IBM Cloud Pak for Data – Test-Drive

By making our comprehensive demo and customer data platform available, we want to offer these customers a way to get a very quick and pragmatic impression of the technology with their data.

Mit Google Cloud passende Lösungen finden
Service

Cloud Development

Finding a standardized solution for complex challenges is unfortunately a rarity. Our experts analyze your problem and present customized solutions.

Bleiben Sie mit dem TIMETOACT GROUP Newsletter auf dem Laufenden!