jupyter sql與python結(jié)合進(jìn)行數(shù)據(jù)分析案例

sql
小樊
100
2024-08-29 03:19:29
欄目: 云計(jì)算

在這個(gè)案例中,我們將使用Jupyter Notebook來結(jié)合SQL和Python進(jìn)行數(shù)據(jù)分析

  1. 首先,確保已經(jīng)安裝了必要的庫。在Jupyter Notebook中運(yùn)行以下命令:
!pip install pandas
!pip install sqlite3
!pip install ipython-sql
  1. 導(dǎo)入所需的庫:
import pandas as pd
import sqlite3
from IPython.display import display
%load_ext sql
  1. 創(chuàng)建一個(gè)SQLite數(shù)據(jù)庫并連接到它:
conn = sqlite3.connect('example.db')
  1. 使用魔法命令%sql連接到數(shù)據(jù)庫:
%sql sqlite:///example.db
  1. 創(chuàng)建一個(gè)表格并插入數(shù)據(jù):
%%sql
CREATE TABLE sales (
    id INTEGER PRIMARY KEY,
    product TEXT,
    price REAL,
    quantity INTEGER,
    date TEXT
);

INSERT INTO sales (product, price, quantity, date) VALUES
    ('Product A', 10.0, 5, '2021-01-01'),
    ('Product B', 20.0, 10, '2021-01-01'),
    ('Product A', 10.0, 15, '2021-01-02'),
    ('Product B', 20.0, 20, '2021-01-02');
  1. 使用SQL查詢數(shù)據(jù)并將結(jié)果存儲(chǔ)在Pandas DataFrame中:
result = %sql SELECT * FROM sales;
df = result.DataFrame()
display(df)
  1. 使用Python對(duì)數(shù)據(jù)進(jìn)行進(jìn)一步處理。例如,計(jì)算每個(gè)產(chǎn)品的總銷售額:
sales_summary = df.groupby('product').sum().reset_index()
sales_summary['total_sales'] = sales_summary['price'] * sales_summary['quantity']
display(sales_summary)
  1. 關(guān)閉數(shù)據(jù)庫連接:
conn.close()

這個(gè)簡單的案例展示了如何在Jupyter Notebook中結(jié)合SQL和Python進(jìn)行數(shù)據(jù)分析。你可以根據(jù)自己的需求修改代碼,以處理更復(fù)雜的數(shù)據(jù)和查詢。

0