Adventure項目1學習記錄

一、導入模塊

導入mysql模塊:

import pymysql
#為了兼容mysqldb,只需要加入
pymysql.install_as_MySQLdb()
#導入sqlalchemy中的create_engine
from sqlalchemy import create_engine

PS:由于之前學習下載ipython包時發(fā)現(xiàn)電腦windows版本較低或網(wǎng)絡環(huán)境較差的原因,導致無法通過pip install 第三方包下載第三方包,因此這次下載pymysql包,直接選擇在Anaconda下載,結果不錯,安裝成功。
具體按照操作:Anaconda Navigator—Environments—apply pymysql包
安裝過程如下圖:


1.1.png

1.2.png

1.3.png

1.4.png

1.5.png

1.6.png

二、讀取數(shù)據(jù)

打開數(shù)據(jù)庫——engine =create_engine('dialect+driver://username:password@host:port/database')

其中:

  • dialect -- 數(shù)據(jù)庫類型
  • driver -- 數(shù)據(jù)庫驅(qū)動選擇
  • username -- 數(shù)據(jù)庫用戶名
  • password -- 用戶密碼
  • host 服務器地址
  • port 端口
  • database 數(shù)據(jù)庫
  • charset=gbk文件編碼格式
    示例:engine = create_engine('mysql+pymysql://project1:mima123@localhost/foo,charset=gbk')
1045問題報錯:
2.1.png
問題原因:數(shù)據(jù)庫用戶名填寫有誤,已解決

三、查看數(shù)據(jù)信息

數(shù)據(jù)字典:
3.1.png
數(shù)據(jù)源初步分析思路:
3.2.png
數(shù)據(jù)源初步操作思路:
3.3.png

四、需注意的操作

1.1.4提取月份維度:
gather_customer_order['create_date'] =pd.to_datetime(gather_customer_order['create_date'])
gather_customer_order['create_year_month']=gather_customer_order['create_date'].astype('str').str[0:7]
1.2.2新增一列order_num_diff,此為每月自行車銷售訂單量環(huán)比,本月與上月對比,例如本期2019-02月銷售額與上一期2019-01月銷售額做對比

第一步:利用diff函數(shù)和列表方法計算環(huán)比

#求每月自行車銷售訂單量環(huán)比,觀察最近一年數(shù)據(jù)變化趨勢
#環(huán)比是本月與上月的對比,例如本期2019-02月銷售額與上一期2019-01月銷售額做對比
order_num_diff = list((overall_sales_performance.order_num.diff()/overall_sales_performance.order_num)-1)
order_num_diff.pop(0)
order_num_diff.append(0)
order_num_diff

第二步:將環(huán)比轉(zhuǎn)化為DataFrame,并合并表格

order_num_diff= pd.DataFrame(order_num_diff)
overall_sales_performance= pd.concat([overall_sales_performance,pd.DataFrame(order_num_diff)],axis=1)

第三步:更改列名,否則顯示列名是0

overall_sales_performance =overall_sales_performance.rename(columns ={0:'order_num_diff'})
ps:diff()函數(shù)用法

df.diff() 內(nèi)部實際先執(zhí)行df.shift(),后執(zhí)行df.shift()-df 兩個操作
diff()函數(shù)原型:
DataFrame.diff(periods=1, axis=0)
參數(shù):
periods:移動的幅度,int類型,默認值為1。
axis:移動的方向,{0 or ‘index’, 1 or ‘columns’},如果為0或者’index’,則上下移動,如果為1或者’columns’,則左右移動。

1.2.4存儲數(shù)據(jù)至mysql:
1054報錯問題:
1.2.4.png
問題原因:表格名重復,現(xiàn)修改,已解決
2.2.2求不同區(qū)域10月11月的環(huán)比:
#1、獲得去重區(qū)域的列表region_list
region_list=list(gather_customer_order_10_11_group.chinese_territory.unique())
region_list
#2、利用for循環(huán)區(qū)域列表,結合loc定位符合區(qū)域,利用pct_change()函數(shù)實現(xiàn)環(huán)比效果,形成新的Series
order_x =pd.Series([])
amount_x =pd.Series([])
for i in region_list:
    a =gather_customer_order_10_11_group.loc[gather_customer_order_10_11_group['chinese_territory']==i]['order_num'].pct_change()
    b=gather_customer_order_10_11_group.loc[gather_customer_order_10_11_group['chinese_territory']==i]['sum_amount'].pct_change()
    order_x=order_x.append(a)
    amount_x = amount_x.append(b)
#3、賦予新的Series的變量名并增加列
gather_customer_order_10_11_group['order_diff']=order_x.
gather_customer_order_10_11_group['amount_diff']=amount_x
#4、由0替換NaN值
gather_customer_order_10_11_group['order_diff']=gather_customer_order_10_11_group['order_diff'].fillna(value =0)
gather_customer_order_10_11_group['amount_diff']=gather_customer_order_10_11_group['amount_diff'].fillna(value =0)
ps:pct_change()函數(shù)

表示當前元素與上一個元素的相差百分比,由于篩選出的只有10-11月的數(shù)據(jù),因此只有11月份同比有數(shù)據(jù),10月無數(shù)據(jù),返回結果會是空值,即NaN,需要在清洗時由0值替換。

2.3.2、將gather_customer_order_11按照chinese_city城市分組,求和銷售數(shù)量order_num,賦予變量gather_customer_order_city_head

一開始寫成這樣報錯(尷尬):

grouped_city=gather_customer_order_11.groupby('chinese_city')
gather_customer_order_city_head=grouped_city['order_num'].agg({'order_num':sum}
1.png

2.png

正解:

grouped_city=gather_customer_order_11.groupby('chinese_city')
gather_customer_order_city_head=pd.DataFrame(grouped_city['order_num'].sum()).reset_index()
3.png
ps:更加理解單列分組聚合與多列分組聚合后返回結果的數(shù)據(jù)結構區(qū)別,即返回后的值為Series還是DataFrame,了解這個,以便于進行后續(xù)操作。
4.1.1 根據(jù)sales_customer_order_11['birth_date'],獲取客人的年份作為新的一列

正解:

sales_customer_order_11['birth_date']= pd.DataFrame((sales_customer_order_11['birth_date'].astype('str').str[0:4]))
4.1.2 sales_customer_order_11['birth_year']字段要求修改為int類型

正解:

sales_customer_order_11['birth_year']=sales_customer_order_11['birth_year'].astype(float).fillna(value=0).astype(int)

做的過程中報錯:ValueError: Cannot convert non-finite values (NA or inf) to integer
原因:原因是['birth_year']列有些記錄是空,所以沒法轉(zhuǎn)成int。
解決:將此列空值填充為0或其他數(shù)字,然后再使用df['birth_year'].astype(int)轉(zhuǎn)換數(shù)據(jù)格式

4.1.3 利用customer_age字段,進行年齡分層,劃分層次為"30-34","35-39","40-44","45-49","50-54","55-59","60-64"
bins =[30,34,39,44,49,54,59,64]
group_names=["30-34","35-39","40-44","45-49","50-54","55-59","60-64"]
sales_customer_order_11['age_level']= pd.cut(sales_customer_order_11['customer_age'],bins=bins,labels =group_names)

PS:注意labels數(shù)量必須少于bins一個,還有bins取范圍(30-34],(34-39]
所以想要表達30-34,,35-39區(qū)間,bins=[30,34,39]?。?!

五、數(shù)據(jù)加工后的圖表數(shù)據(jù)字典

圖.png
最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內(nèi)容