项目    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))

 

posted on 2022-04-15 23:22  Genius$LW  阅读(54)  评论(0编辑  收藏  举报