2003031118—李伟—Python数据分析第七周作业—MySQL的安装以及使用
| 项目 | MySQL的安装以及使用 |
| 课程班级博客链接 | 20级数据班(本) |
| 这个作业要求链接 | 作业要求 |
| 博客名称 | 2003031118—李伟—Python数据分析第七周作业—MySQL的安装以及使用 |
| 要求 | 每道题要有题目,代码(使用插入代码,不会插入代码的自己查资料解决,不要直接截图代码!!),截图(只截运行结果)。 |
1.安装好MySQL,连接上Navicat。
*扩展阅读:小白必看!超详细MySQL下载安装教程
*扩展阅读:MySQL教程
- *扩展阅读:MySQL卸载
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的安装以及使用的更多相关文章
- 2003031121——浦娟——Python数据分析第七周作业——MySQL的安装及使用
项目 要求 课程班级博客链接 20级数据班(本) 作业要求链接 Python第七周作业 博客名称 2003031121--浦娟--Python数据分析第七周作业--MySQL的安装及使用 要求 每道题 ...
- 2003031121-浦娟-python数据分析第三周作业-第一次作业
项目 内容 课程班级博客链接 https://edu.cnblogs.com/campus/pexy/20sj 作业链接 https://edu.cnblogs.com/campus/pexy/20s ...
- 2017-2018-1 我爱学Java 第六七周 作业
团队六七周作业 完善版需求规格说明书 制定团队编码规范 数据库设计 后端架构设计 TODOList 参考资料 完善版需求规格说明书 <需求规格说明书>初稿不足之处: 1.开发工具写错 2. ...
- 2018-2019-1 20189221 《Linux内核原理与分析》第七周作业
2018-2019-1 20189221 <Linux内核原理与分析>第七周作业 实验六 分析Linux内核创建一个新进程的过程 代码分析 task_struct: struct task ...
- 2017-2018-1 JAVA实验站 第六、七周作业
2017-2018-1 JAVA实验站 第六.七周作业 详情请见团队博客
- 2017-2018-1 JaWorld 第六、七周作业
2017-2018-1 JaWorld 第六.七周作业 修改需求规格说明书 上次的<需求规格说明书>初稿有哪些不足? 王译潇同学回答: 1. 引言和目的性考虑的不是很周全. 2. ...
- 2017-2018-1 20179205《Linux内核原理与设计》第七周作业
<Linux内核原理与设计>第七周作业 视频学习及操作分析 创建一个新进程在内核中的执行过程 fork.vfork和clone三个系统调用都可以创建一个新进程,而且都是通过调用do_for ...
- 2019-2020-1 20199325《Linux内核原理与分析》第七周作业
第七周作业 1.进程描述符task_struct数据结构(一) 为了管理进程,内核必须对每个进程进行清晰的描述,进程描述符提供了内核所需了解的进程信息. struct task_struct数据结构很 ...
- 2019-2020-1 20199329《Linux内核原理与分析》第七周作业
<Linux内核原理与分析>第七周作业 一.本周内容概述: 对Linux系统如何创建一个新进程进行追踪 分析Linux内核创建一个新进程的过程 二.本周学习内容: 1.学习进程的描述 操作 ...
- 2020-2021-1 20209307《Linux内核原理与分析》第七周作业
这个作业属于哪个课程 <2020-2021-1Linux内核原理与分析)> 这个作业要求在哪里 <2020-2021-1Linux内核原理与分析第七周作业> 这个作业的目标 & ...
随机推荐
- Linux系统root密码忘记修改
重启Linux系统 到启动状态: 按任意键,进入GRUB菜单: 按字母e进入: 调整到上图,按字母e,进入 不进行删减,在后面输入" 1"(空格1),按回车,进入 或者将" ...
- 关于iptables的一些知识
关闭firewalld, 启用iptables:systemctl stop firewalld && systemctl disable firewalld # 关闭firewa ...
- python获取当前运行函数名
两种方式: #!/usr/bin/env python3#coding:utf-8 import sys, inspectdef test_a(): print('func name: ', sys. ...
- vue element-ui form验证中自定义验证方式通过不返回true问题
项目中使用了element-ui的form验证,自定义了手机号的验证规则,验证不通过的时候定义了callback()扔出错误.但是忘了写通过的callback().导致form验证通过拿不到返回的va ...
- Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:3.0.0-M5:test (default-test) on project
updated to from jdk16 Java jdk 8 or 11 to support jenkins Build run. Plugin management tags aligned ...
- 【剑指Offer】【数组】顺时针打印矩阵
题目:输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下4 X 4矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数字1 ...
- C++实现单链表相关操作
#include<iostream>#include<cstdlib>//C++动态分配存储空间using namespace std;#define OK 1#define ...
- 如何利用python的xlrd模块读取日期格式的Excel
经常使用python操作Excel,就会遇到各种坑,比如,有时候你读取到的某一单元格的数据,你预想的结果本来应该是这样的,但是它却是这样的,真是很蛋疼.于是你会找各种解决办法,去解决这个问题!所以鄙人 ...
- Java基础——二维数组
package com.zhao.demo; public class Demo08 { public static void main(String[] args) { //二维数组 int[][] ...
- Git commit时提示错误时 解决办法
问题描述: * Please tell me who you are. Run git config –global user.email "you@example.com" ...