開始使用免費開始

資料庫綱要

到現在為止,你已經知道 SQL 資料庫一定會有資料庫綱要。在資料庫的影片中,你看過下列圖表:

Database Schema for Customer and Order

你的本機環境已設定一個 PostgreSQL 資料庫,包含這個資料庫綱要,並填入了一些範例資料。你可以使用 pandasread_sql() 函式來查詢資料庫。你需要傳入一個資料庫引擎,這個引擎已為你定義為 db_engine

pandas 套件以 pd 匯入後,會把查詢結果存成一個 DataFrame 物件,因此從資料庫取回結果後,你就可以對它使用任何 DataFrame 的功能。

本練習屬於課程

Data Engineering 入門

檢視課程

練習說明

  • 完成 SELECT 敘述,從 "Customer" 資料表選取 first_namelast_name。請先依 lastname,再依 firstname 排序。
  • 使用 .head() 方法顯示 data 的前 3 列。
  • 使用 .info() 顯示 data 的一般資訊。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Complete the SELECT statement
data = pd.read_sql("""
SELECT first_name, ____ FROM "____"
ORDER BY ____, ____
""", db_engine)

# Show the first 3 rows of the DataFrame
print(data.head(____))

# Show the info of the DataFrame
print(data.____())
編輯並執行程式碼