使用Pandas处理MySQL中的数据


目录:

使用Pandas链接MySQL

安装pymysql+sqlalchemy

 pip install pymysql sqlalchemy

简单案例

数据下载

import os
import pandas as pd
from sqlalchemy import create_engine
## 创建一个mysql连接器,用户名为root,密码为root123
## 地址为127.0.0.1,数据库名称为testdb,编码为utf-8
engine = create_engine(
    'mysql+pymysql://root:root123@127.0.0.1:3306/testdb?charset=utf8')

engine
Engine(mysql+pymysql://root:***@127.0.0.1:3306/testdb?charset=utf8)
## 使用read_sql_query查看tesdb中的数据表数目
table_list = pd.read_sql_query('show tables', con = engine)
table_list
Tables_in_testdb
0 meal_order_detail1
## 使用read_sql_table读取订单详情表
detail1 = pd.read_sql_table('meal_order_detail1',con = engine)
len(detail1)
2779
detail1.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 2779 entries, 0 to 2778
Data columns (total 19 columns):
 #   Column             Non-Null Count  Dtype         
---  ------             --------------  -----         
 0   detail_id          2779 non-null   object        
 1   order_id           2779 non-null   object        
 2   dishes_id          2779 non-null   object        
 3   logicprn_name      2779 non-null   object        
 4   parent_class_name  2779 non-null   object        
 5   dishes_name        2779 non-null   object        
 6   itemis_add         2779 non-null   object        
 7   counts             2779 non-null   float64       
 8   amounts            2779 non-null   float64       
 9   cost               2779 non-null   object        
 10  place_order_time   2779 non-null   datetime64[ns]
 11  discount_amt       2779 non-null   object        
 12  discount_reason    2779 non-null   object        
 13  kick_back          2779 non-null   object        
 14  add_inprice        2779 non-null   object        
 15  add_info           2779 non-null   object        
 16  bar_code           2779 non-null   object        
 17  picture_file       2779 non-null   object        
 18  emp_id             2779 non-null   object        
dtypes: datetime64[ns](1), float64(2), object(16)
memory usage: 412.6+ KB
detail1.describe()
counts amounts
count 2779.000000 2779.000000
mean 1.111191 45.337172
std 0.625428 36.808550
min 1.000000 1.000000
25% 1.000000 25.000000
50% 1.000000 35.000000
75% 1.000000 56.000000
max 10.000000 178.000000
## 使用read_sql读取订单详情表
detail2 = pd.read_sql('select * from meal_order_detail2',
      con = engine)
len(detail2)
3647
detail3 = pd.read_sql('meal_order_detail3',con = engine)
len(detail3)
3611
detail4 = pd.read_sql('select dishes_name from  meal_order_detail3 limit 10,10;',con = engine)
detail4
dishes_name
0 豌豆薯仔猪骨汤
1 倒立蒸梭子蟹
2 葱姜炒蟹
3 芝士烩波士顿龙虾
4 豌豆薯仔猪骨汤
5 芝士烩波士顿龙虾
6 焖猪手
7 焖猪手
8 自制猪肉脯
9 香菇鸡肉粥
detail4.to_csv("meal_order.csv")