第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

首頁 慕課教程 Pandas 入門教程 Pandas 入門教程 Pandas 讀取 MySql 數(shù)據(jù)

Pandas 讀取 MySql 數(shù)據(jù)

1. 前言

上節(jié)課我們講述了 Pandas 解析 TXT 文件,CSV 文件以及 Excel 文件數(shù)據(jù),主要涉及到了兩個函數(shù) read_csv() 函數(shù)和 read_excel() 函數(shù),并詳細(xì)講解了對應(yīng)函數(shù)中常用的參數(shù)設(shè)置。

那 MySQL 作為數(shù)據(jù)記錄和處理的常用工具之一,我們?nèi)绾斡?Pandas 進(jìn)行 MySQL 數(shù)據(jù)的解析呢?本節(jié)課我們首先講述 PyMySQL 庫進(jìn)行 MySQL 數(shù)據(jù)庫的連接,然后講述 Pandas 對 MySQLl 數(shù)據(jù)庫的讀取。

2. 安裝 PyMySQL 庫

PyMySQL 庫是用于連接 MySQL 服務(wù)器的一個庫,對應(yīng)的是 Python3.x 的版本,如果是 Python2 要使用 MySqlDb 庫。我們在使用 PyMySQL 之前要確保已經(jīng)安裝了該模塊,下面我們介紹一下如何安裝 PyMySQL 庫。

打開我們的 Anaconda 文件下的 Anaconda Prompt 工具,然后在命令窗口中輸入命令行:pip install PyMySQL 接著按回車鍵,可以看到安裝進(jìn)度,最后顯示 Successfully… 表示安裝成功:

圖片描述

3. 準(zhǔn)備數(shù)據(jù)文件

首先我們在 Navicate 數(shù)據(jù)庫可視化管理工具中,新建數(shù)據(jù)庫 example ,然后在該數(shù)據(jù)庫中新建數(shù)據(jù)表 t_file (該表是文件管理數(shù)據(jù)表),在里面我們已經(jīng)準(zhǔn)備了一些數(shù)據(jù):

圖片描述

4. PyMySQL 庫的使用

4.1 導(dǎo)入 PyMySQL 庫

打開我們的 Notebook 工具,創(chuàng)建新的 Python 工作文件,通過 import pymysql 在程序中導(dǎo)入PyMySQL 庫:

圖片描述

4.2 connect() 函數(shù)

PyMySQL 庫中的函數(shù) connect() 用于連接數(shù)據(jù)庫,返回的對象是一個數(shù)據(jù)庫連接對象,下面我們列舉出了該方法常用的參數(shù)介紹:

參數(shù)名稱 描述
host 連接的數(shù)據(jù)庫地址
port 數(shù)據(jù)庫的端口號,默認(rèn)的是 3306
database 要連接的數(shù)據(jù)庫名稱
user 數(shù)據(jù)庫用戶名
password 數(shù)據(jù)庫密碼
charset 字符編碼方式

下面我們通過代碼用 connect() 方法來連接我們剛才建立的 example 數(shù)據(jù)庫:

# 導(dǎo)入pandas 和 pymysql 包
import pandas as pd
import pymysql
# 返回一個 Connection 對象
db_conn = pymysql.connect(
host='localhost',
port=3306,
user='root',
password='****',
database='example',
charset='utf8'
)
print(db_conn)

# --- 輸出結(jié)果 ---
<pymysql.connections.Connection object at 0x000002C6F1D4F8E0>

輸出解析:通過輸出結(jié)果可以看到,我們使用 connect() 函數(shù),連接 MySQL 數(shù)據(jù)庫,返回的是個 pymysql.connections.Connection 數(shù)據(jù)庫連接對象,通過該對象我們后面可以進(jìn)行對數(shù)據(jù)庫的操作 。

5. Pandas 解析 MySql 數(shù)據(jù)

上面我們通過 PyMySQL 庫建立了 MySQL 數(shù)據(jù)庫連接對象,接下來我們將通過 Pandas 進(jìn)行 MySQL 數(shù)據(jù)的解析。

5.1 read_sql() 函數(shù)

該函數(shù)主要從數(shù)據(jù)庫中讀取數(shù)據(jù)幀,里面提供了一些參數(shù)可以設(shè)置,下面列舉常用的幾個參數(shù):

參數(shù)名稱 描述
sql 要執(zhí)行的 sql 語句
con 數(shù)據(jù)庫連接對象
params 參數(shù)的傳遞
index_col 選擇某一列作為 index

參數(shù) sql 和參數(shù) con

我們首先通過代碼演示這兩個參數(shù)的設(shè)置,讀取 example 數(shù)據(jù)庫中的 t_file 數(shù)據(jù)表數(shù)據(jù):

# 導(dǎo)入pandas 和 pymysql 包
import pandas as pd
import pymysql
# 返回一個 Connection 對象
db_conn = pymysql.connect(
host='localhost',
port=3306,
user='root',
password='0508',
database='example',
charset='utf8'
)

# 執(zhí)行sql操作
sql="select * from t_file"
pd.read_sql(sql,con=db_conn)

# --- 輸出結(jié)果 ---

	id	directory	fileName	md5	size	time	type
0   1 C:/temp-rainy1	匯報(bào).png	845a500eca6ef877	22507	2020-01-16 10:53:02	png
1   2 C:/temp-rainy2	模板.png	845a500eca6ef877	22507	2020-01-16 10:59:16	png
2   3 C:/temp-rainy3	說明書.docx  5626d69f9f39d65d	235999	2020-01-16 10:59:45	docx
3   4 C:/temp-rainy4	接口及調(diào)用說明.rar	459e008250ccb9ca1 391542 2020-01-16 11:02:07 rar
4   5 C:/temp-rainy5	數(shù)據(jù)說明.pdf	333052beda8773	434439	2020-01-16 13:44:03	pdf

輸出解析:我們通過 sql 語句設(shè)置數(shù)據(jù)庫操作語句,這里我們是查詢 t_file 的所有數(shù)據(jù),參數(shù) con 中我們傳入了 PyMySQL 庫創(chuàng)建的 db_con 數(shù)據(jù)庫連接對象。通過輸出結(jié)果可以看到,查出了 t_file 中的數(shù)據(jù)內(nèi)容。

參數(shù) params

該參數(shù)用于我們執(zhí)行 sql 中參數(shù)的配置:

# 導(dǎo)入pandas 和 pymysql 包
import pandas as pd
import pymysql
# 返回一個 Connection 對象
db_conn = pymysql.connect(
host='localhost',
port=3306,
user='root',
password='0508',
database='example',
charset='utf8'
)

# 執(zhí)行sql操作
sql = "select * from t_file where id = %s"
pd.read_sql(sql,con=db_conn,params=[2])

# --- 輸出結(jié)果 ---

	id	directory	fileName	md5	size	time	type
0	2	C:/temp-rainy2	模板.png	845a500eca6ef877	22507	2020-01-16 10:59:16	png

輸出解析:我們 sql 查詢語句中設(shè)置了 id 的參數(shù)條件,在 read_sql() 函數(shù)中我們通過 params 參數(shù)設(shè)置了 id 的條件為 2,因此看到我們的數(shù)據(jù)結(jié)果,正是我們數(shù)據(jù)庫中 id=2 的數(shù)據(jù)行。

參數(shù) index_col

通過該參數(shù)的設(shè)置,某一列指定為行索引。

# 導(dǎo)入pandas 和 pymysql 包
import pandas as pd
import pymysql
# 返回一個 Connection 對象
db_conn = pymysql.connect(
host='localhost',
port=3306,
user='root',
password='0508',
database='example',
charset='utf8'
)

# 執(zhí)行sql操作
sql = "select * from t_file"
pd.read_sql(sql,con=db_conn,index_col="type")

# --- 輸出結(jié)果 ---
	id	directory	fileName	md5	size	time
type						
png	1	C:/temp-rainy1	匯報(bào).png	845a500eca6ef877	22507	2020-01-16 10:53:02
png	2	C:/temp-rainy2	模板.png	845a500eca6ef877	22507	2020-01-16 10:59:16
docx	3	C:/temp-rainy3	說明書.docx	5626d69f9f39d65d	235999	2020-01-16 10:59:45
rar	4	C:/temp-rainy4	接口及調(diào)用說明.rar	459e008250ccb9ca1	391542	2020-01-16 11:02:07
pdf	5	C:/temp-rainy5	數(shù)據(jù)說明.pdf	333052beda8773	434439	2020-01-16 13:44:03

輸出解析:程序中我們通過設(shè)置 index_col=“type” ,指定字段文件的類型 (type) 作為我們數(shù)據(jù)行的索引,這里可以看到輸出結(jié)果,最左側(cè)的一列正式我們的 type 數(shù)據(jù)列,如果不指定 index_col ,默認(rèn)的行索引是從0開始進(jìn)行序號遞增編排。

3. 小結(jié)

以上就是我們通過 Pandas 讀取 MySql 數(shù)據(jù)庫數(shù)據(jù)的基本操作,本節(jié)課程我們主要學(xué)習(xí)了 PyMySQL 庫進(jìn)行數(shù)據(jù)庫連接對象的創(chuàng)建,然后用 Pandas 中的 read_sql() 函數(shù)進(jìn)行數(shù)據(jù)庫數(shù)據(jù)的解析,以及該函數(shù)中幾個常用的參數(shù)。本節(jié)課程的重點(diǎn)如下:

  • PyMySQL 庫的安裝和數(shù)據(jù)庫連接函數(shù) connect() 的使用;
  • Pandas 庫中 read_sql() 函數(shù)的使用和里面常用的參數(shù)。

圖片描述