表中数据来自于《统计学习方法》第二版P71页
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;
INSERT INTO loan_application
(Age,job_state,house_state,credit_state,category)
VALUES
("青年","否","否","一般","否"),
("青年","否","否","好","否"),
("青年","是","否","好","是"),
("青年","是","是","一般","是"),
("青年","否","否","一般","否"),
("中年","否","否","一般","否"),
("中年","否","否","好","否"),
("中年","是","是","好","是"),
("中年","否","是","非常好","是"),
("中年","否","是","非常好","是"),
("老年","否","是","非常好","是"),
("老年","否","是","好","是"),
("老年","是","否","好","是"),
("老年","是","否","非常好","是"),
("老年","否","否","一般","否");
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)
# 导包
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) # 从数据库中导入数据表
******
处应该输入自己设置的密码
pd.read_sql()用法