利用Python连接MySQL将表单转化为DataFrame

利用Python连接MySQL将表单转化为DataFrame

表中数据来自于《统计学习方法》第二版P71页

1. 创建loan_application表单

CREATE TABLE loan_application(
	ID int NOT NULL AUTO_INCREMENT,
	Age char(2) NOT NULL,
	job_state char(1) NOT NULL,
	house_state char(1) NOT NULL,
	credit_state varchar(4) NOT NULL,
	category char(1) NOT NULL,
	PRIMARY KEY (ID)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;

2. 插入多条数据

 INSERT INTO loan_application
    (Age,job_state,house_state,credit_state,category)
    VALUES
    ("青年","否","否","一般","否"),
    ("青年","否","否","好","否"),
    ("青年","是","否","好","是"),
    ("青年","是","是","一般","是"),
    ("青年","否","否","一般","否"),
    ("中年","否","否","一般","否"),
    ("中年","否","否","好","否"),
    ("中年","是","是","好","是"),
    ("中年","否","是","非常好","是"),
    ("中年","否","是","非常好","是"),
    ("老年","否","是","非常好","是"),
    ("老年","否","是","好","是"),
    ("老年","是","否","好","是"),
    ("老年","是","否","非常好","是"),
    ("老年","否","否","一般","否");

3. 查看表

select * from loan_application;

结果

+----+--------+-----------+-------------+--------------+----------+
| ID | Age    | job_state | house_state | credit_state | category |
+----+--------+-----------+-------------+--------------+----------+
|  1 | 青年   ||| 一般         ||
|  2 | 青年   |||||
|  3 | 青年   |||||
|  4 | 青年   ||| 一般         ||
|  5 | 青年   ||| 一般         ||
|  6 | 中年   ||| 一般         ||
|  7 | 中年   |||||
|  8 | 中年   |||||
|  9 | 中年   ||| 非常好       ||
| 10 | 中年   ||| 非常好       ||
| 11 | 老年   ||| 非常好       ||
| 12 | 老年   |||||
| 13 | 老年   |||||
| 14 | 老年   ||| 非常好       ||
| 15 | 老年   ||| 一般         ||
+----+--------+-----------+-------------+--------------+----------+
15 rows in set (0.00 sec)

4. 连接数据转化DataFrame

# 导包
import pandas as pd
import numpy as np
import pymysql
from sqlalchemy import create_engine

# 数据库类型+数据库驱动名称://用户名:口令@机器地址:端口号/数据库名
engine = create_engine('mysql+pymysql://root:*******@127.0.0.1:3306/mydatabase?charset=utf8')
df = pd.read_sql('select * from loan_application',engine) # 从数据库中导入数据表

******处应该输入自己设置的密码

表格预览:
利用Python连接MySQL将表单转化为DataFrame_第1张图片

pd.read_sql()用法

你可能感兴趣的:(MySQL,sql,python,Pandas,create_engine)