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/10/23

Part 1: Data Analysis with ChatGPT

In this new blog series we will give you an overview of how to analyze and visualize data, create code manually and how to make ChatGPT work effectively. Part 1 deals with the following: In the data-driven era, businesses and organizations are constantly seeking ways to extract meaningful insights from their data. One powerful tool that can facilitate this process is ChatGPT, a state-of-the-art natural language processing model developed by OpenAI. In Part 1 pf this blog, we'll explore the proper usage of data analysis with ChatGPT and how it can help you make the most of your data.

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 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.

Blog

Part 2: TIMETOACT Logistics Hackathon

Learn how TIMETOACT’s hackathon team built a Python logistics simulator, modeling maps, truck speeds, and fastest-route algorithms to optimize transport planning.

Workation bei catworkx Teil 2 - Head of Sales Dirk hat es auch ausprobiert
Blog

Buzzword Workation’ Part II - An interview with Dirk

Workation at catworkx – this time with Dirk, our Head of Sales. He talks about his experience working under the Spanish sun and why he would do it again in a heartbeat.

Blog

Part 3: TIMETOACT Logistics Hackathon

Extend logistics simulations with a speed model: predict travel times using historical data, Python, and polynomial regression for realistic traffic insights.

Blog

Using Historical Data to Simulate Truck Journey

Discover how historical truck data and Python simulations can predict journey times and CO₂ emissions, helping logistics become smarter and greener.

Blog 8/7/20

Understanding F# Type Aliases

In this post, we discuss the difference between F# types and aliases that from a glance may appear to be the same thing.

Articifial Intelligence & Data Science
Service

Artificial Intelligence & Data Science

Data Science is all about extracting valuable information from structured and unstructured data.

Blog 9/17/21

How to gather data from Miro

Learn how to gather data from Miro boards with this step-by-step guide. Streamline your data collection for deeper insights.

Referenz

Implementation of an application lifecycle management

The EOS Group managed its requirements management in a database for a long time. With Jira and catworkx, implementation requirements were transferred and application lifecycle management optimized.

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.

Referenz

Standardized data management creates basis for reporting

TIMETOACT implements a higher-level data model in a data warehouse for TRUMPF Photonic Components and provides the necessary data integration connection with Talend.

Blog 9/13/22

Introduction to Functional Programming in F# – Part 2

Explore functions, types, and modules in F#. Enhance your skills with practical examples and insights in this detailed guide.

Blog 7/14/21

Building and Publishing Design Systems | Part 2

Learn how to build and publish design systems effectively. Discover best practices for creating reusable components and enhancing UI consistency.

Blog 12/22/22

Introduction to Functional Programming in F# – Part 7

Explore LINQ and query expressions in F#. Simplify data manipulation and enhance your functional programming skills with this guide.

News 11/13/24

25 years of catworkx

25 years of catworkx – a quarter of a century of success and innovation! We celebrated this special anniversary in style on a Danube cruise: from Passau via Vienna to Budapest and back, we took time to look back together on 25 years of developments and successes. A big thank you to our fantastic team and everyone who has joined us on this journey – we look forward to the next 25 years with you!

Schild als Symbol für innere und äußere Sicherheit
Branche

Internal and external security

Defense forces and police must protect citizens and the state from ever new threats. Modern IT & software solutions support them in this task.

Bleiben Sie mit dem TIMETOACT GROUP Newsletter auf dem Laufenden!