Python数据分析第七周作业

发布时间:2022-06-24 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了Python数据分析第七周作业脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
项目 内容
课程班级博客链接 20级数据班(本)
这个作业要求链接  Python数据分析第七周作业
博客名称 2003031124—邱云龙—Python数据分析第七周作业
要求 每道题要有题目,代码(使用插入代码,不会插入代码的自己查资料解决,不要直接截图代码!!),截图(只截运行结果)。
  • 1.安装好MySQL,连接上Navicat。
  • 2.完成课本练习(代码4-1~3/4-9~31)。

代码4-1~3:

from sqlalchemy import create_engine
#创建一个MySQL连接器,用户名为root,密码为525811
from sqlalchemy import create_engine
#创建一个MySQL连接器,用户名为root,密码为525811
#地址为127.0.0.1数据库名称为testdb,编码为UTF—8
engine = create_engine('mysql+pymysql://root:525811@127.0.0.1:3306/testdb?charset=utf8')
print(engine)

import pandas as pd
#使用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_table读取订单详情表
detail2 = pd.read_sql('select * from meal_order_detail2',con = engine)
print('使用read_sql+SQL语句读取订单详情表的长度为:',len(detail2))
detail3 = pd.read_sql('meal_order_detail3',con = engine)
print('使用read_sql+表格名称读取订单详情表的长度为:',len(detail3))

#使用to_sql存储orderDate
detail1.to_sql('test1',con = engine,index = False,if_exists = 'replace')
#使用read_sql读取test表
formlist1 = pd.read_sql_query('show tables',con = engine)
print('新增一个表格后,testdb数据库数据清单为:','n',formlist1)

  

Python数据分析第七周作业

 

4-9~31:

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

#使用read_sql_table读取订单详情表
order1 = pd.read_sql_table('meal_order_detail1',con = engine)
print('订单详情表1的长度为:',len(order1))
order2 = pd.read_sql_table('meal_order_detail2',con = engine)
print('订单详情表2的长度为:',len(order2))
order3 = pd.read_sql_table('meal_order_detail3',con = engine)
print('订单详情表3的长度为:',len(order3))

#使用read_table读取订单信息表
orderInfo = pd.read_table('D:/meal_order_info.csv',sep = ',',encoding = 'gbk')
print('订单信息表的长度为:',len(orderInfo))

userInfo = pd.read_excel('D:/meal_order_detail.xlsx',sheet_name = 'users1')
print('客户信息表的长度为:',len(userInfo))

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)'''

#查看DataFrame的元素个数
print('订单详情表的元素个数为:',detail.size)
print('订单详情表的维度数为:',detail.ndim)#查看DataFrame的维度数
print('订单详情表的形状为:',detail.shape)#查看DataFrame的形状

print('订单详情表转置前形状为:',detail.shape)
print('订单详情表转置后形状为:',detail.T.shape)

#使用字典访问的方式取出orderInfo中的某一列
order_id = detail['order_id']
print('订单详情表中的order_id形状为:','n',order_id.shape)

#使用访问属性的方式取出orderInfo中的菜品名称列
dishes_name = detail.dishes_name
print('订单详情表中的dishes_name的形状为:','n',dishes_name.shape)

dishes_name5 = detail['dishes_name'][:5]
print('订单详情表中的dishes_name前5个元素为;','n',dishes_name5)

orderDish = detail[['order_id','dishes_name']][:5]
print('订单详情表中的order_id和dishes_name前5个元素为;','n',orderDish)

order5 = detail[:][1:6]
print('订单详情表中的1~6元素为;','n',order5)

print('订单详情表中的前5行数据为;','n',detail.head())
print('订单详情表中的后5行数据为;','n',detail.tail())

dishes_name1 = detail.loc[:,'dishes_name']
print('使用loc提取dishes_name列的size为:',dishes_name1.size)
dishes_name2 = detail.iloc[:,3]
print('使用iloc提取第3列列的size为:',dishes_name2.size)

orderDish1 = detail.loc[:,['order_id','dishes_name']]
print('使用loc提取order_id和dishes_name列地size为;',orderDish1.size)
orderDish2 = detail.iloc[:,[1,3]]
print('使用iloc提取第1列和第3列地size为;',orderDish2.size)

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]])

#loc内部传入表达式
print('detail中order_id为458的dishes_name为:n',detail.loc[detail['order_id']=='458',['order_id','dishes_name']])
#错误示例如下:
#print('detail中order_id为458的第1、5列数据为:n',detail.iloc[detail['order_id']=='458',[1,5]])

print('detail中order_id为458的第1,5列数据为:n',detail.iloc[(detail['order_id']=='458').values,[1,5]])

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进行了升级和重构。

#将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'])

detail['payment'] = detail['counts']*detail['amounts']
print('detail新增列payment的前5行为:','n',detail['payment'].head())

detail['pay_way'] = '现金支付'
print('detail新增列pay_way的前5行为:','n',detail['pay_way'].head())

print('删除pay_way前detail的列索引为:','n',detail.columns)
detail.drop(labels = 'pay_way',axis = 1,inplace = True)
print('删除pay_way后detail的列索引为:','n',detail.columns)

print('删除1~10行前detail的长度为:',len(detail))
detail.drop(labels = range(1,11),axis = 0,inplace = True)
print('删除1~10行后detail的长度为:',len(detail))

  

Python数据分析第七周作业

 

Python数据分析第七周作业

 

Python数据分析第七周作业

 

Python数据分析第七周作业

 

Python数据分析第七周作业

 

Python数据分析第七周作业

 

Python数据分析第七周作业

 

Python数据分析第七周作业

 

 

Python数据分析第七周作业

 

 

 

 

 

 

 

 

 

 

 

from sqlalchemy import create_engine#创建一个MySQL连接器,用户名为root,密码为root1234#地址为127.0.0.1数据库名称为testdb,编码为UTF—8engine = create_engine('mysql+pymysql://525811@127.0.0.1:3306/testdb?charset=utf8')import pandas as pd#使用read_sql_table读取订单详情表order1 = pd.read_sql_table('meal_order_detail1',con = engine)print('订单详情表1的长度为:',len(order1))order2 = pd.read_sql_table('meal_order_detail2',con = engine)print('订单详情表2的长度为:',len(order2))order3 = pd.read_sql_table('meal_order_detail3',con = engine)print('订单详情表3的长度为:',len(order3))#使用read_table读取订单信息表orderInfo = pd.read_table('D:/meal_order_info.csv',sep = ',',encoding = 'gbk')print('订单信息表的长度为:',len(orderInfo))userInfo = pd.read_excel('D:/meal_order_detail.xlsx',sheet_name = 'users1')print('客户信息表的长度为:',len(userInfo))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)'''#查看DataFrame的元素个数print('订单详情表的元素个数为:',detail.size)print('订单详情表的维度数为:',detail.ndim)#查看DataFrame的维度数print('订单详情表的形状为:',detail.shape)#查看DataFrame的形状print('订单详情表转置前形状为:',detail.shape)print('订单详情表转置后形状为:',detail.T.shape)#使用字典访问的方式取出orderInfo中的某一列order_id = detail['order_id']print('订单详情表中的order_id形状为:','n',order_id.shape)#使用访问属性的方式取出orderInfo中的菜品名称列dishes_name = detail.dishes_nameprint('订单详情表中的dishes_name的形状为:','n',dishes_name.shape)dishes_name5 = detail['dishes_name'][:5]print('订单详情表中的dishes_name前5个元素为;','n',dishes_name5)orderDish = detail[['order_id','dishes_name']][:5]print('订单详情表中的order_id和dishes_name前5个元素为;','n',orderDish)order5 = detail[:][1:6]print('订单详情表中的1~6元素为;','n',order5)print('订单详情表中的前5行数据为;','n',detail.head())print('订单详情表中的后5行数据为;','n',detail.tail())dishes_name1 = detail.loc[:,'dishes_name']print('使用loc提取dishes_name列的size为:',dishes_name1.size)dishes_name2 = detail.iloc[:,3]print('使用iloc提取第3列列的size为:',dishes_name2.size)orderDish1 = detail.loc[:,['order_id','dishes_name']]print('使用loc提取order_id和dishes_name列地size为;',orderDish1.size)orderDish2 = detail.iloc[:,[1,3]]print('使用iloc提取第1列和第3列地size为;',orderDish2.size)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]])#loc内部传入表达式print('detail中order_id为458的dishes_name为:n',detail.loc[detail['order_id']=='458',['order_id','dishes_name']])#错误示例如下:#print('detail中order_id为458的第1、5列数据为:n',detail.iloc[detail['order_id']=='458',[1,5]])print('detail中order_id为458的第1,5列数据为:n',detail.iloc[(detail['order_id']=='458').values,[1,5]])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进行了升级和重构。#将ordeer_id为458的变换为45800detail.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'])detail['payment'] = detail['counts']*detail['amounts']print('detail新增列payment的前5行为:','n',detail['payment'].head())detail['pay_way'] = '现金支付'print('detail新增列pay_way的前5行为:','n',detail['pay_way'].head())print('删除pay_way前detail的列索引为:','n',detail.columns)detail.drop(labels = 'pay_way',axis = 1,inplace = True)print('删除pay_way后detail的列索引为:','n',detail.columns)print('删除1~10行前detail的长度为:',len(detail))detail.drop(labels = range(1,11),axis = 0,inplace = True)print('删除1~10行后detail的长度为:',len(detail))

脚本宝典总结

以上是脚本宝典为你收集整理的Python数据分析第七周作业全部内容,希望文章能够帮你解决Python数据分析第七周作业所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: