Computer >> Máy Tính >  >> Lập trình >> Python

Làm thế nào để quét qua các tệp phương tiện trong Python?

Giới thiệu

Trong môi trường kinh doanh công ty thế giới thực, hầu hết dữ liệu có thể không được lưu trữ trong tệp văn bản hoặc tệp Excel. Cơ sở dữ liệu quan hệ dựa trên SQL như Oracle, SQL Server, PostgreSQL và MySQL đang được sử dụng rộng rãi và nhiều cơ sở dữ liệu thay thế đã trở nên khá phổ biến.

Việc lựa chọn cơ sở dữ liệu thường phụ thuộc vào nhu cầu về hiệu suất, tính toàn vẹn của dữ liệu và khả năng mở rộng của một ứng dụng.

Cách thực hiện ..

Trong ví dụ này, chúng ta sẽ làm thế nào để tạo một cơ sở dữ liệu sqlite3. sqllite được cài đặt theo mặc định với cài đặt python và không yêu cầu cài đặt thêm. Nếu bạn không chắc chắn, vui lòng thử bên dưới. Chúng tôi cũng sẽ nhập gấu trúc.

Việc tải dữ liệu từ SQL vào DataFrame khá đơn giản và gấu trúc có một số chức năng để đơn giản hóa quy trình.

import sqlite3
import pandas as pd
print(f"Output \n {sqlite3.version}")

Đầu ra

2.6.0

Đầu ra

# connection object
conn = sqlite3.connect("example.db")
# customers data
customers = pd.DataFrame({
"customerID" : ["a1", "b1", "c1", "d1"]
, "firstName" : ["Person1", "Person2", "Person3", "Person4"]
, "state" : ["VIC", "NSW", "QLD", "WA"]
})
print(f"Output \n *** Customers info -\n {customers}")

Đầu ra

*** Customers info -
customerID firstName state
0 a1 Person1 VIC
1 b1 Person2 NSW
2 c1 Person3 QLD
3 d1 Person4 WA
# orders data
orders = pd.DataFrame({
"customerID" : ["a1", "a1", "a1", "d1", "c1", "c1"]
, "productName" : ["road bike", "mountain bike", "helmet", "gloves", "road bike", "glasses"]
})

print(f"Output \n *** orders info -\n {orders}")

Đầu ra

*** orders info -
customerID productName
0 a1 road bike
1 a1 mountain bike
2 a1 helmet
3 d1 gloves
4 c1 road bike
5 c1 glasses
# write to the db
customers.to_sql("customers", con=conn, if_exists="replace", index=False)
orders.to_sql("orders", conn, if_exists="replace", index=False)

Đầu ra

# frame an sql to fetch the data.
q = """
select orders.customerID, customers.firstName, count(*) as productQuantity
from orders
left join customers
on orders.customerID = customers.customerID
group by customers.firstName;
"""

Đầu ra

# run the sql.
pd.read_sql_query(q, con=conn)

Ví dụ

7. Ghép tất cả lại với nhau.

import sqlite3
import pandas as pd
print(f"Output \n {sqlite3.version}")
# connection object
conn = sqlite3.connect("example.db")
# customers data
customers = pd.DataFrame({
"customerID" : ["a1", "b1", "c1", "d1"]
, "firstName" : ["Person1", "Person2", "Person3", "Person4"]
, "state" : ["VIC", "NSW", "QLD", "WA"]
})

print(f"*** Customers info -\n {customers}")

# orders data
orders = pd.DataFrame({
"customerID" : ["a1", "a1", "a1", "d1", "c1", "c1"]
, "productName" : ["road bike", "mountain bike", "helmet", "gloves", "road bike", "glasses"]
})

print(f"*** orders info -\n {orders}")

# write to the db
customers.to_sql("customers", con=conn, if_exists="replace", index=False)
orders.to_sql("orders", conn, if_exists="replace", index=False)

# frame an sql to fetch the data.
q = """
select orders.customerID, customers.firstName, count(*) as productQuantity
from orders
left join customers
on orders.customerID = customers.customerID
group by customers.firstName;

"""

# run the sql.
pd.read_sql_query(q, con=conn)

Đầu ra

2.6.0
*** Customers info -
customerID firstName state
0 a1 Person1 VIC
1 b1 Person2 NSW
2 c1 Person3 QLD
3 d1 Person4 WA
*** orders info -
customerID productName
0 a1 road bike
1 a1 mountain bike
2 a1 helmet
3 d1 gloves
4 c1 road bike
5 c1 glasses
customerID firstName productQuantity
____________________________________
0      a1         Person1     3
1 c1 Person3 2
2 d1 Person4 1