zl程序教程

您现在的位置是:首页 >  数据库

当前栏目

【深度学习】pandas操作mysql

mysqlpandas学习 操作 深度
2023-09-27 14:20:13 时间

sqlalchemy的形式

import pandas
import sqlalchemy

engine = sqlalchemy.create_engine('mssql+pymssql://用户名:密码@地址:端口号/表名')
sql = "select max(rid) as maxrid FROM student "
connection = engine.connect()
df = pandas.read_sql(sql=sql, con=connection)
print(df)

pymysql的形式


sql = "select dominant_tree_type, sum(small_class_accumulation) as total_accumulation from carbon_sink_bigscreen.res_forest_class_info where dominant_tree_type is not NULL and small_class_accumulation > 0 group by dominant_tree_type"
connection = db_conn = pymysql.connect(
host='10.10.xxx.xx',
port=3306,
user='root',
password='111111',
database='xxxx',
charset='utf8')

df = pd.read_sql_query(sql, connection)
print(df)
  dominant_tree_type  total_accumulation
0                 桉树           2777804.0
1                湿地松             43321.0
2                阔叶树              3654.0

打印的结果