项目    MySQL的安装以及使用
课程班级博客链接 20级数据班(本)
这个作业要求链接 作业要求
博客名称 2003031118—李伟—Python数据分析第七周作业—MySQL的安装以及使用
要求 每道题要有题目,代码(使用插入代码,不会插入代码的自己查资料解决,不要直接截图代码!!),截图(只截运行结果)。

1.安装好MySQL,连接上Navicat。

2.完成课本练习(代码4-1~3/4-9~31)。(4-1至4-3是一个,4-9至4-11是一个,4-12至4-31是一个)说明:4-12至4-31分开运行,最后有总的

代码4-1至代码4-3

 1 from sqlalchemy import create_engine
2 engine=create_engine("mysql+pymysql://root:root@127.0.0.1:3306/testdb?charset=utf8")
3 print(engine)
4
5 import pandas as pd
6 #使用read_sql_query查看testdb中的数据表书目
7 formlist=pd.read_sql_query('show tables',con=engine)
8 print('testdb数据表清单为:","\n ',formlist)
9
10 detail1=pd.read_sql_table('meal_order_detail1',con=engine)
11 print("使用read_sql_query读取清单的长度为:",len(detail1))
12
13 detail2=pd.read_sql('select*from meal_order_detail2',con=engine)
14 print("使用read_sql函数+SQL语句读取的订单详情表长度为:",len(detail2))
15
16 detail3=pd.read_sql('meal_order_detail3',con=engine)
17 print('使用read_sql函数+SQL语句读取的订单详情表长度为:',len(detail3))
18
19 detail1.to_sql('test1',con=engine,index=False,if_exists='replace')
20 formlist1=pd.read_sql_query('show tables',con=engine)
21 print('新增一个表格后,testdb数据表清单为:“,”\n',formlist1)

 代码4-9至代码4-11

 1 from sqlalchemy import create_engine
2 engine=create_engine("mysql+pymysql://root:root@127.0.0.1:3306/testdb?charset=utf8")
3 import pandas as pd
4 order1=pd.read_sql_table('meal_order_detail1',con=engine)
5 print("订单详情表1的长度为:",len(order1))
6 order2=pd.read_sql_table('meal_order_detail2',con=engine)
7 print("订单详情表2的长度为:",len(order2))
8 order3=pd.read_sql_table('meal_order_detail3',con=engine)
9 print("订单详情表3的长度为:",len(order3))
10 order4=pd.read_table('D:/桌面/第四章/meal_order_info.csv',sep=",",encoding='gbk')
11 print('订单信息表的长度为:',len(order4))
12 user=pd.read_excel('D:/桌面/第四章/users.xlsx')
13 print('客户信息表的长度为:',len(user))

 代码4-12至4-14

 1 from sqlalchemy import create_engine
2 engine=create_engine("mysql+pymysql://root:root@127.0.0.1:3306/testdb?charset=utf8")
3 print(engine)
4
5 import pandas as pd
6 #使用read_sql_query查看testdb中的数据表书目
7 detail=pd.read_sql_table('meal_order_detail1',con=engine)
8 print('订单详情表的索引为:',detail.index)
9 print('订单详情表的所有值为:“,”\n',detail.values)
10 print('订单详情表的列名为:“,”\n',detail.columns)
11 print('订单详情表的数据类型为:“,”\n',detail.dtypes)
12 print('订单详情表的元素个数为:“,”\n',detail.size)
13 print('订单详情表的维度数为:“,”\n',detail.ndim)
14 print('订单详情表的形状为:“,”\n',detail.shape)
15 print('订单详情表转置前形状为:“,”\n',detail.shape)
16 print('订单详情表转置后形状为:“,”\n',detail.T.shape)

代码4-15至4-19

 1 from sqlalchemy import create_engine
2 engine=create_engine("mysql+pymysql://root:root@127.0.0.1:3306/testdb?charset=utf8")
3 print(engine)
4
5 import pandas as pd
6 #使用read_sql_query查看testdb中的数据表书目
7 detail=pd.read_sql_table('meal_order_detail1',con=engine)
8
9 order_id=detail['order_id']
10 print("订单详情表中的order_id的形状为:','\n",order_id.shape)
11 dishes_name=detail.dishes_name
12 print('订单详情表中的dishes_name的形状为:',dishes_name.shape)
13 dishes_name5=detail['dishes_name'][:5]
14 print('订单详情表中的dishes_name前5个的元素为:',dishes_name5)
15 orderDish=detail[['order_id','dishes_name']][:5]
16 print("订单详情表中的order_id和dishes_name前5个的元素为:","\n",orderDish)
17 order5=detail[:][1:6]
18 print('订单详情表的1——6行元素为:","\n',order5)

 代码4-20至4-23

 1 from sqlalchemy import create_engine
2 engine=create_engine("mysql+pymysql://root:root@127.0.0.1:3306/testdb?charset=utf8")
3 print(engine)
4
5 import pandas as pd
6 #使用read_sql_query查看testdb中的数据表书目
7 detail=pd.read_sql_table('meal_order_detail1',con=engine)
8 print('订单详情表中前5行数据为:","\n',detail.head())
9 print('订单详情表中后5行数据为:","\n',detail.tail())
10
11 dishes_name1=detail.loc[:,'dishes_name']
12 print('使用loc提取dishes_name列的size为:',dishes_name1.size)
13 dishes_name2=detail.iloc[:,3]
14 print('使用loc提取第3列的size为:',dishes_name2.size)
15
16 orderDish1=detail.loc[:,['order_id','dishes_name']]
17 print('使用loc提取order_id和dishes_name列的size为:',orderDish1.size)
18 orderDish2=detail.iloc[:,[1,3]]
19 print('使用iloc提取第1列和第3列的size为:',orderDish2.size)
20 print('列名为order_id和dishes_name的行名为3的数据为:\n',detail.loc[3,['order_id','dishes_name']])
21 print('列名为order_id和dishes_name的行名为2,3,4,5,6的数据为:\n',detail.loc[2:6,['order_id','dishes_name']])
22 print('列位置为1和3,行位置为3的数据为:\n',detail.iloc[3,[1,3]])
23 print('列位置为1和3,行位置为2,3,4,5,6的数据为:\n',detail.iloc[2:7,[1,3]])

 代码4-24至4-27

 1 from sqlalchemy import create_engine
2 engine=create_engine("mysql+pymysql://root:root@127.0.0.1:3306/testdb?charset=utf8")
3 print(engine)
4
5 import pandas as pd
6 #使用read_sql_query查看testdb中的数据表书目
7 detail=pd.read_sql_table('meal_order_detail1',con=engine)
8
9 print('detail中order为458的dishes_name为;\n',detail.loc[detail['order_id']=='458',['order_id','dishes_name']])
10 print('detail中order_id为458的1、5列数据为:\n',detail.iloc[(detail['order_id']=='458').values,[1,5]])
11 print('列名为dishes_name行名为2,3,4,5,6的数据为:\n',detail.loc[2:6,'dishes_name'])
12 print('列位置为5,行位置为2-6的数据为:\n',detail.iloc[2:6,5])
13 #print('列位置为5,行名为2-6的数据为:","\n',detail.ix[2:6,5])
14 detail.loc[detail['order_id']=='458','order_id']='45800'
15 print('更改后detail中order_id为458的order_id为:\n',detail.loc[detail['order_id']=='458','order_id'])
16 print('更改后detail中order_id为45800的order_id为:\n',detail.loc[detail['order_id']=='45800','order_id'])

 代码4-28至4-31

 1 from sqlalchemy import create_engine
2 engine=create_engine("mysql+pymysql://root:root@127.0.0.1:3306/testdb?charset=utf8")
3 print(engine)
4
5 import pandas as pd
6 #使用read_sql_query查看testdb中的数据表书目
7 detail=pd.read_sql_table('meal_order_detail1',con=engine)
8
9 detail['payment']=detail['counts']*detail['amounts']
10 print('detail新增列payment的前5行为:","\n',detail['payment'].head())
11 detail['pay_way']='现金支付'
12 print('detail新增列pay-way的前5行为;","\n',detail['pay_way'].head())
13 print('删除pay_way前detail的列索引为;","\n',detail.columns)
14 detail.drop(labels='pay_way',axis=1,inplace=True)
15 print('删除pay_way后detail的列索引为:","\n',detail.columns)
16 print('删除1-10行前detail的长度为:',len(detail))
17 detail.drop(labels=range(1,11),axis=0,inplace=True)
18 print('删除1-10行后detail的长度为:',len(detail))

代码 4-12至4-31

 1 from sqlalchemy import create_engine
2 engine=create_engine("mysql+pymysql://root:root@127.0.0.1:3306/testdb?charset=utf8")
3 print(engine)
4
5 import pandas as pd
6 #使用read_sql_query查看testdb中的数据表书目
7 detail=pd.read_sql_table('meal_order_detail1',con=engine)
8 print('订单详情表的索引为:',detail.index)
9 print('订单详情表的所有值为:“,”\n',detail.values)
10 print('订单详情表的列名为:“,”\n',detail.columns)
11 print('订单详情表的数据类型为:“,”\n',detail.dtypes)
12 print('订单详情表的元素个数为:“,”\n',detail.size)
13 print('订单详情表的维度数为:“,”\n',detail.ndim)
14 print('订单详情表的形状为:“,”\n',detail.shape)
15 print('订单详情表转置前形状为:“,”\n',detail.shape)
16 print('订单详情表转置后形状为:“,”\n',detail.T.shape)
17
18 order_id=detail['order_id']
19 print("订单详情表中的order_id的形状为:','\n",order_id.shape)
20 dishes_name=detail.dishes_name
21 print('订单详情表中的dishes_name的形状为:',dishes_name.shape)
22 dishes_name5=detail['dishes_name'][:5]
23 print('订单详情表中的dishes_name前5个的元素为:',dishes_name5)
24 orderDish=detail[['order_id','dishes_name']][:5]
25 print("订单详情表中的order_id和dishes_name前5个的元素为:","\n",orderDish)
26 order5=detail[:][1:6]
27 print('订单详情表的1——6行元素为:","\n',order5)
28
29 print('订单详情表中前5行数据为:","\n',detail.head())
30 print('订单详情表中后5行数据为:","\n',detail.tail())
31
32 dishes_name1=detail.loc[:,'dishes_name']
33 print('使用loc提取dishes_name列的size为:',dishes_name1.size)
34 dishes_name2=detail.iloc[:,3]
35 print('使用loc提取第3列的size为:',dishes_name2.size)
36
37 orderDish1=detail.loc[:,['order_id','dishes_name']]
38 print('使用loc提取order_id和dishes_name列的size为:',orderDish1.size)
39 orderDish2=detail.iloc[:,[1,3]]
40 print('使用iloc提取第1列和第3列的size为:',orderDish2.size)
41 print('列名为order_id和dishes_name的行名为3的数据为:\n',detail.loc[3,['order_id','dishes_name']])
42 print('列名为order_id和dishes_name的行名为2,3,4,5,6的数据为:\n',detail.loc[2:6,['order_id','dishes_name']])
43 print('列位置为1和3,行位置为3的数据为:\n',detail.iloc[3,[1,3]])
44 print('列位置为1和3,行位置为2,3,4,5,6的数据为:\n',detail.iloc[2:7,[1,3]])
45
46 print('detail中order为458的dishes_name为;\n',detail.loc[detail['order_id']=='458',['order_id','dishes_name']])
47 print('detail中order_id为458的1、5列数据为:\n',detail.iloc[(detail['order_id']=='458').values,[1,5]])
48 print('列名为dishes_name行名为2,3,4,5,6的数据为:\n',detail.loc[2:6,'dishes_name'])
49 print('列位置为5,行位置为2-6的数据为:\n',detail.iloc[2:6,5])
50 #print('列位置为5,行名为2-6的数据为:","\n',detail.ix[2:6,5])
51 detail.loc[detail['order_id']=='458','order_id']='45800'
52 print('更改后detail中order_id为458的order_id为:\n',detail.loc[detail['order_id']=='458','order_id'])
53 print('更改后detail中order_id为45800的order_id为:\n',detail.loc[detail['order_id']=='45800','order_id'])
54
55 detail['payment']=detail['counts']*detail['amounts']
56 print('detail新增列payment的前5行为:","\n',detail['payment'].head())
57 detail['pay_way']='现金支付'
58 print('detail新增列pay-way的前5行为;","\n',detail['pay_way'].head())
59 print('删除pay_way前detail的列索引为;","\n',detail.columns)
60 detail.drop(labels='pay_way',axis=1,inplace=True)
61 print('删除pay_way后detail的列索引为:","\n',detail.columns)
62 print('删除1-10行前detail的长度为:',len(detail))
63 detail.drop(labels=range(1,11),axis=0,inplace=True)
64 print('删除1-10行后detail的长度为:',len(detail))

2003031118—李伟—Python数据分析第七周作业—MySQL的安装以及使用的更多相关文章

  1. 2003031121——浦娟——Python数据分析第七周作业——MySQL的安装及使用

    项目 要求 课程班级博客链接 20级数据班(本) 作业要求链接 Python第七周作业 博客名称 2003031121--浦娟--Python数据分析第七周作业--MySQL的安装及使用 要求 每道题 ...

  2. 2003031121-浦娟-python数据分析第三周作业-第一次作业

    项目 内容 课程班级博客链接 https://edu.cnblogs.com/campus/pexy/20sj 作业链接 https://edu.cnblogs.com/campus/pexy/20s ...

  3. 2017-2018-1 我爱学Java 第六七周 作业

    团队六七周作业 完善版需求规格说明书 制定团队编码规范 数据库设计 后端架构设计 TODOList 参考资料 完善版需求规格说明书 <需求规格说明书>初稿不足之处: 1.开发工具写错 2. ...

  4. 2018-2019-1 20189221 《Linux内核原理与分析》第七周作业

    2018-2019-1 20189221 <Linux内核原理与分析>第七周作业 实验六 分析Linux内核创建一个新进程的过程 代码分析 task_struct: struct task ...

  5. 2017-2018-1 JAVA实验站 第六、七周作业

    2017-2018-1 JAVA实验站 第六.七周作业 详情请见团队博客

  6. 2017-2018-1 JaWorld 第六、七周作业

    2017-2018-1 JaWorld 第六.七周作业 修改需求规格说明书 上次的<需求规格说明书>初稿有哪些不足? 王译潇同学回答:   1. 引言和目的性考虑的不是很周全.   2. ...

  7. 2017-2018-1 20179205《Linux内核原理与设计》第七周作业

    <Linux内核原理与设计>第七周作业 视频学习及操作分析 创建一个新进程在内核中的执行过程 fork.vfork和clone三个系统调用都可以创建一个新进程,而且都是通过调用do_for ...

  8. 2019-2020-1 20199325《Linux内核原理与分析》第七周作业

    第七周作业 1.进程描述符task_struct数据结构(一) 为了管理进程,内核必须对每个进程进行清晰的描述,进程描述符提供了内核所需了解的进程信息. struct task_struct数据结构很 ...

  9. 2019-2020-1 20199329《Linux内核原理与分析》第七周作业

    <Linux内核原理与分析>第七周作业 一.本周内容概述: 对Linux系统如何创建一个新进程进行追踪 分析Linux内核创建一个新进程的过程 二.本周学习内容: 1.学习进程的描述 操作 ...

  10. 2020-2021-1 20209307《Linux内核原理与分析》第七周作业

    这个作业属于哪个课程 <2020-2021-1Linux内核原理与分析)> 这个作业要求在哪里 <2020-2021-1Linux内核原理与分析第七周作业> 这个作业的目标 & ...

随机推荐

  1. 【git】3.5 git分支-远程分支

    资料来源 (1) https://git-scm.com/book/zh/v2/Git-%E5%88%86%E6%94%AF-%E8%BF%9C%E7%A8%8B%E5%88%86%E6%94%AF ...

  2. 什么是cache

    什么是cacheTo minimize the quantity of control information stored, the spatial locality property is use ...

  3. Fortran笔记之过程重载,多态,泛型编程

    参考自Introduction to Modern Fortran for the Earth System Sciences 过程重载 OOP中的另一个重要技术是过程重载(Procedure Ove ...

  4. STM32F0使用LL库实现UART接收

    初始化: 1 void MX_USART1_UART_Init(void) 2 { 3 4 /* USER CODE BEGIN USART1_Init 0 */ 5 6 /* USER CODE E ...

  5. Windows Defender锁定UI

    计算机\HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows Defender Security Center 看下这个项下的子项有没有被添加值 ...

  6. 查看Sql数据库连接数

    select count(1) as nums, sd.name from  [Master].[dbo].[SYSPROCESSES] spinner join [Master].[dbo].[SY ...

  7. SVN安装配置手册

    1.官网下载相关的服务端的安装包 SVN(版本控制器): SVN-客户端:TortoiseSVN SVN-服务端:VisualSVN 下载地址: TortoiseSVN:https://tortois ...

  8. 2345 ip

    121.201.101.43 img1.2345.com121.201.101.43 img2.2345.com121.201.101.43 img3.2345.com121.201.101.43 i ...

  9. CF1067E 题解

    题意 传送门 给定一棵 \(n\) 个节点的树,每条边有 \(\frac{1}{2}\) 的概率出现,可以得到一个森林,求这个森林邻接矩阵的秩的期望. \(1\le n\le5\times10^5\) ...

  10. PPT模板网址

    PPT免费下载网址: https://www.1ppt.com/ PPT模板下载:www.1ppt.com/moban/                行业PPT模板:www.1ppt.com/han ...