MySQL的安装以及使用

 

python作业:

  • 1.安装好MySQL,连接上Navicat。
  • 2.完成课本练习(代码4-1~3/4-9~31)。

阅读体会:通过阅读,了解了MySQL的下载安装教程,为初学者的我们提供了很多帮助,从下载到安装使用以及如何卸载都做了很详细的解读和引导。

作业1:安装好MySQL,连接上Navicat。

作业2:完成课本练习(代码4-1~3/4-9~31)。

#4-1
#读取订单详情表
from sqlalchemy import create_engine
import pandas as pd
 
 
#创建一个MySQL连接器
engine = create_engine('mysql+pymysql://root:201314@127.0.0.1:3306/testdb?charset=utf8')
 
print(engine)
 
 
 
#4-2
#使用read_sql_query查看testdb中的数据表数目
formlist=pd.read_sql_query('show tables',con = engine)
#print('testdb数据库表数据清单为:','\n',formlist)
 
#使用read_sql_table读取订单详情表
detail1=pd.read_sql_table('meal_order_detail1',con=engine)
print('使用read_sql_table读取订单详情表的长度为:',len(detail1))
 
#使用read_sql读取订单详情表
detail2=pd.read_sql('select*from meal_order_detail2',con = engine)
print('使用read_sql函数+SQL语句读取的订单详情表的长度为:',len(detail2))
 
detail3=detail3=pd.read_sql('meal_order_detail3',con = engine)
print('使用read_sql函数+表格名称读取的订单详情表的长度为:',len(detail3))
 
 
#4-3
#读取订单详情表
from sqlalchemy import create_engine
import pandas as pd
 
engine = create_engine('mysql+pymysql://root:201314@127.0.0.1:3306/testdb?charset=utf8')
 
 
detail1=pd.read_sql_table('meal_order_detail1',con=engine)
detail1.to_sql('test1',con = engine,index = False,if_exists = 'replace')
 
 
formlist1 = pd.read_sql_query('show tables',con = engine)
print('新增一个表格后,testdb数据库数据表清单为:','\n',formlist1)

代码运行结果截图

7d524d028287c471faa586a45456f6b9.png

14c76c6aa1913c3499c1892a597ef67a.png

9f9972353fa3f3e6c56f691100e749a3.png

e6b99ad7df62d3aca0dcedd95e8e46f6.png

作业2—练习4-9~31代码

#4-9
#读取订单详情表
from sqlalchemy import create_engine
import pandas as pd
 
engine = create_engine('mysql+pymysql://root:201314@127.0.0.1:3306/testdb?charset=utf8')
 
order1 = pd.read_sql('meal_order_detail1', con=engine)
print('订单详情表一的长度为:',len(order1))
 
order2 = pd.read_sql('meal_order_detail2', con=engine)
print('订单详情表二的长度为:',len(order2))
 
order3 = pd.read_sql('meal_order_detail3', con=engine)
print('订单详情表三的长度为:',len(order3))
 
 
#4-10
#使用read_table读取订单信息表
from sqlalchemy import create_engine
import pandas as pd
 
 
orderInfo = pd.read_table('D:/Users/Administrator/Desktop/meal_order_info.csv',sep=',',encoding='gbk')
 
print('订单信息表长度为:',len(orderInfo))
 
 
 
#4-11
#读取users.xlsx文件
from sqlalchemy import create_engine
import pandas as pd
 
 
userInfo = pd.read_excel('D:/Users/Administrator/Desktop/users.xlsx')
 
print('客户信息表长度为:',len(userInfo))
 
 
 
#4-12
#订单详情表的四个基本属性
from sqlalchemy import create_engine
import pandas as pd
 
#创建数据库连接
engine = create_engine('mysql+pymysql://root:201314@127.0.0.1:3306/testdb?charset=utf8')
detail = pd.read_sql_table('meal_order_detail1',con=engine)
 
print('订单详情表的索引为:',detail.index)
print('订单详情表的所有值为:','\n',detail.values)
print('订单详情表的列名为:','\n',detail.columns)
print('订单详情表的数据类型为:\n',detail.dtypes)
 
 
#4-13 size、ndim、shape属性的使用
 
from sqlalchemy import create_engine
import pandas as pd
 
#创建数据库连接
engine = create_engine('mysql+pymysql://root:201314@127.0.0.1:3306/testdb?charset=utf8')
detail = pd.read_sql_table('meal_order_detail1',con=engine)
 
#查看dataframe的元素个数
print('订单详情表的元素个数为:',detail.size)
 
#查看dataframe的维度数
print('订单详情表的元素个数为:',detail.ndim)
 
#查看dataframe的形状
print('订单详情表的元素个数为:',detail.shape)
 
 
 
#4-14 使用T属性进行转换
 
from sqlalchemy import create_engine
import pandas as pd
 
#创建数据库连接
engine = create_engine('mysql+pymysql://root:201314@127.0.0.1:3306/testdb?charset=utf8')
detail = pd.read_sql_table('meal_order_detail1',con=engine)
 
 
print('订单详情表转置前形状为:',detail.shape)
 
print('订单详情表转置后形状为:',detail.T.shape)
 
#4-15 使用字典访问内部数据的方式访问dataframe中的一列
from sqlalchemy import create_engine
import pandas as pd
 
#创建数据库连接
engine = create_engine('mysql+pymysql://root:201314@127.0.0.1:3306/testdb?charset=utf8')
detail = pd.read_sql_table('meal_order_detail1',con=engine)
 
#取出某一列
order_id = detail['order_id']
 
print('订单详情表中的order_id的形状为:','\n',order_id.shape)
 
#4-16 使用访问属性的方式访问orderInfo中的菜品名称列
dishes_name = detail.dishes_name
print('订单详情表中的dishes_name的形状为:','\n',dishes_name.shape)
 
#4-17 dataframe单列多行数据获取
dishes_name5 = detail['dishes_name'][:5]
print('订单详情表中的dishes_name前5个元素为:','\n',dishes_name5)
 
#4-18 访问dataframe多列的多行数据
orderDish = detail[['order_id','dishes_name']][:5]
print('订单详情表中的order_id和dishesname前5个元素为:','\n',orderDish)
 
#4-19 访问dataframe多行数据
order5 = detail[:][1:6]
print('订单详情表的1-6行元素为:','\n',order5)
 
#4-20 使用dataframe的head和tail方法获取多行数据
print('订单详情表中前5行数据为:','\n',detail.head())
print('订单详情表中后5行元素为:','\n',detail.tail())
 
#4-21 使用loc和iloc实现单列切片
dishes_name1 = detail.loc[:,'dishes_name']
print('使用loc提取dishes_name列的size为:',dishes_name1.size)
 
dishes_name2 = detail.iloc[:,3]
print('使用iloc提取第三列的size为:',dishes_name2.size)
 
#4-22 使用loc和iloc实现多列切片
orderDish1 = detail.loc[:,['order_id','dishes_name']]
print('使用loc提取order_id和dishes_name列的size为:',orderDish1.size)
 
orderDish2 = detail.iloc[:,[1,3]]
print('使用iloc提取第一列和第三列的size为:',orderDish2.size)
 
#4-23 使用loc和iloc实现花式切片
print('列名为order_id和dishes_name的行名为3的数据为:','\n',detail.loc[3,['order_id','dishes_name']])
print('列名为order_id和dishes_name的行名为2,3,4,5,6的数据为:','\n',detail.loc[2:6,['order_id','dishes_name']])
print('列位置为1和3,行位置为3的数据为:\n',detail.iloc[3,[1,3]])
print('列位置为1和3,行位置为2,3,4,5,6的数据为:\n',detail.iloc[2:7,[1,3]])
 
#4-24 使用loc和和iloc实现条件切片
print('detail中order为order_id为458的dishes_name为:\n',detail.loc[detail['order_id']=='458',['order_id','dishes_name']])
#print('detail中order为order_id为458的第1、5列数据为:\n',detail.iloc[detail['order_id']=='458',[1,5]])
#NotImplementedError: iLocation based boolean indexing on an integer type is not available
 
#4-25 使用iloc实现条件切片
print('detail中order_id为458的第1,5列数据为:\n',detail.iloc[(detail['order_id']=='458').values,[1,5]])
 
#4-26 使用loc、iloc、ix实现切片比较
print('列名为dishes_name行名为 2,3,4,5,6的数据为:\n',detail.loc[2:6,'dishes_name'])
print('列位置为5,行位置为2~6的数据为:\n',detail.iloc[2:6,5])
#print('列位置为5,行名为2~6的数据为:', '\n',detail.ix[2:6,5])    #pandas的1.0.0版本后,已经对ix进行了升级和重构。
 
#4-27 更改dataframe中的数据
# 将ordeer_id为458的变换为45800
detail.loc[detail['order_id']=='458','ordeer_id'] = '45800'
print('更改后detail中order_id为458的order_id为:\n',detail.loc[detail['order_id']=='458','order_id'])
print('更改后detail中order_id为45800的order_id为:\n',detail.loc[detail['order_id']=='45800','order_id'])
 
#4-28 为dataframe新增一列非定值
detail['payment'] = detail['counts']*detail['amounts']
print('detail新增列payment的前5行为:','\n',detail['payment'].head())
 
#4-29 dataframe新增一列定值
detail['pay_way'] = '现金支付'
print('detail新增列pay_way的前5行为:','\n',detail['pay_way'].head())
print('删除pay_way前detail的列索引为:','\n',detail.columns)
 
#4-30 删除dataframe某列
detail.drop(labels = 'pay_way',axis = 1,inplace = True)
print('删除pay_way后detail的列索引为:','\n',detail.columns)
 
#4-31 删除dataframe某几行
print('删除1~10行前detail的长度为:',len(detail))
detail.drop(labels = range(1,11),axis = 0,inplace = True)
print('删除1~10行后detail的长度为:',len(detail))

运行结果截图

8510a47c5d2718e02d2900874d327111.png

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

鹿毅十川

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值