| 項目 | 內容 |
| 課程班級博客鏈接 | https://edu.cnblogs.com/campus/pexy/20sj |
| 這個作業要求鏈接 | https://edu.cnblogs.com/campus/pexy/20sj/homework/12597 |
| 博客名稱 | 2003031120—廖威—Python數據分析第七周作業—MySQL的安裝以及使用 |
| 要求 | 每道題要有題目,代碼(使用插入代碼,不會插入代碼的自己查資料解決,不要直接截圖代碼!!),截圖(只截運行結果) |
題目一:擴展閱讀心得體會
1.擴展閱讀:小白必看!超詳細MySQL下載安裝教程
2.擴展閱讀:MySQL教程
3.擴展閱讀:MySQL卸載
題目二:
1.安裝好MySQL,連接上Navicat。
2.完成課本練習(代碼4-1~3/4-9~31)。
from sqlalchemy import create_engine #創建一個MYSQL連接器,用戶名為root,密碼為111 #地址為127.0.0.1,數據庫名稱為testdb,編碼為UTF-8 engine = create_engine('mysql+pymysql://root:111@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讀取訂單詳情表 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存儲orderData 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)
#導入SQLAIchemy庫的creat_engine函數 from sqlalchemy import create_engine import pandas as pd #創建一個MYSQL連接器,用戶名為root,密碼為111 #地址為127.0.0.1,數據庫名稱為testdb engine = create_engine('mysql+pymysql://root:111@127.0.0.1:3306/testdb?charset=utf8') #使用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:/Users/ASUS/Desktop/meal_order_info.csv',sep = ',',encoding = 'gbk') print('訂單信息表長度為:',len(orderInfo)) #讀取users.xlsx文件 userInfo = pd.read_excel('D:/Users/ASUS/Desktop/users.xlsx') print('客戶信息表長度為:',len(userInfo)) from sqlalchemy import create_engine import pandas as pd engine = create_engine('mysql+pymysql://root:111@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) #查看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的形狀為:',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[:][:6] print('訂單詳情表的1~6行元素為:','\n',order5) print('訂單詳情表中前5行數據為:','\n',detail.head()) print('訂單詳情表中后5行數據為:','\n',detail.tail()) dishes_namel = detail.loc[:,'dishes_name'] print('使用loc提取dishes_name列的size為:',dishes_namel.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]])#返回的為一個布爾值Series,而iloc可以接收的數據類型并不包括Series 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版本開始,移除了Series.ix and DataFrame.ix 方法。使用DataFrame的loc方法或者iloc方法進行替換吧。 #將order_id為458的變換45800 detail.loc[detail['order_id']=='458','order_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))
運行結果














浙公網安備 33010602011771號