Python數(shù)據(jù)分析與可視化典型項(xiàng)目實(shí)戰(zhàn)模塊八-電商客戶消費(fèi)偏好特征分析_第1頁(yè)
Python數(shù)據(jù)分析與可視化典型項(xiàng)目實(shí)戰(zhàn)模塊八-電商客戶消費(fèi)偏好特征分析_第2頁(yè)
Python數(shù)據(jù)分析與可視化典型項(xiàng)目實(shí)戰(zhàn)模塊八-電商客戶消費(fèi)偏好特征分析_第3頁(yè)
Python數(shù)據(jù)分析與可視化典型項(xiàng)目實(shí)戰(zhàn)模塊八-電商客戶消費(fèi)偏好特征分析_第4頁(yè)
Python數(shù)據(jù)分析與可視化典型項(xiàng)目實(shí)戰(zhàn)模塊八-電商客戶消費(fèi)偏好特征分析_第5頁(yè)
已閱讀5頁(yè),還剩127頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

模塊八電商客戶消費(fèi)偏好特征分析Python數(shù)據(jù)分析與可視化典型項(xiàng)目實(shí)戰(zhàn)(微課版)【任務(wù)描述】現(xiàn)有3個(gè)Excel文件,分別是“df_short.xlsx”(記錄“客戶消費(fèi)信息”)、“df_short_buy.xlsx”(記錄“客戶消費(fèi)信息”并增加統(tǒng)計(jì)數(shù)量列)、“df_label.xlsx”(記錄?!翱蛻羝锰卣鳌保@些數(shù)據(jù)是京東電子商務(wù)網(wǎng)站74天的交易數(shù)據(jù)。其中“df_short.xlsx”文件有8列、55148行數(shù)據(jù),所有列都不存在數(shù)據(jù)缺失情況;“df_label.xlsx”文件有21列、49003行數(shù)據(jù),僅有“Unnamed:0”“customer_id”“browse_not_buy”“cart_not_buy”“buy_again”這5列無(wú)數(shù)據(jù)缺失情況,有16列數(shù)據(jù)存在缺失情況。其中列名“Unnamed:0”表示臨時(shí)列名,暫用“Unnamed:0”表示。針對(duì)df_short和df_label兩個(gè)數(shù)據(jù)集完成以下主要操作。(1)查看數(shù)據(jù)集的相關(guān)信息。(2)對(duì)數(shù)據(jù)集進(jìn)行數(shù)據(jù)預(yù)處理。(3)計(jì)算客戶統(tǒng)計(jì)總數(shù)量。(4)計(jì)算客戶消費(fèi)主要電商指標(biāo)。(5)統(tǒng)計(jì)客戶瀏覽數(shù)量與購(gòu)買(mǎi)數(shù)量?!救蝿?wù)8-1】京東客戶消費(fèi)數(shù)據(jù)預(yù)處理與整體消費(fèi)特征分析【任務(wù)實(shí)現(xiàn)】在JupyterNotebook開(kāi)發(fā)環(huán)境中創(chuàng)建tc08-01.ipynb,然后在單元格中編寫(xiě)代碼并輸出對(duì)應(yīng)的結(jié)果?!救蝿?wù)8-1】京東客戶消費(fèi)數(shù)據(jù)預(yù)處理與整體消費(fèi)特征分析京東客戶消費(fèi)數(shù)據(jù)預(yù)處理與整體消費(fèi)特征分析1.導(dǎo)入模塊導(dǎo)入通用模塊的代碼詳見(jiàn)“本書(shū)導(dǎo)學(xué)”。data1=pd.read_excel(r'data\df_short.xlsx',parse_dates=['date'],dtype={'customer_id':str,'product_id':str})df_short=data1.copy()2.讀取數(shù)據(jù)讀取客戶消費(fèi)信息數(shù)據(jù)集的代碼如下:data2=pd.read_excel(r'.\data\df_label.xlsx',dtype={'customer_id':str})df_label=data2讀取客戶偏好特征數(shù)據(jù)集的代碼如下:京東客戶消費(fèi)數(shù)據(jù)預(yù)處理與整體消費(fèi)特征分析3.查看數(shù)據(jù)集df_short的相關(guān)信息查看數(shù)據(jù)集df_short的基本信息df_()查看數(shù)據(jù)集df_short的大?。ňS度)print('數(shù)據(jù)集大小:',df_short.shape)查看數(shù)據(jù)集df_short的部分?jǐn)?shù)據(jù)隨機(jī)查看數(shù)據(jù)集的5行數(shù)據(jù)的代碼如下:df_short.sample(5)同時(shí)展示前5行和后5行數(shù)據(jù)的代碼如下:pd.concat([df_short.head(),df_short.tail()])京東客戶消費(fèi)數(shù)據(jù)預(yù)處理與整體消費(fèi)特征分析查看“date”列的基本信息df_short['date'].describe(datetime_is_numeric=True)查看時(shí)間范圍,并輸出起始日期、結(jié)束日期及日期相差minDate=df_short['date'].min()print('起始日期:',minDate)maxDate=df_short['date'].max()print('結(jié)束日期:',maxDate)diffData=maxDate-minDateprint('日期相差:',diffData,'天')print('時(shí)間范圍為:{}到{}'.format(min(data1.date),max(data1.date)))刪除無(wú)效字段刪除“Unnamed:0”列的代碼如下:df_short.drop(df_short.columns[[0]],axis=1,inplace=True)京東客戶消費(fèi)數(shù)據(jù)預(yù)處理與整體消費(fèi)特征分析以下代碼也能實(shí)現(xiàn)刪除“Unnamed:0”列的功能:short_df.drop(labels='Unnamed:0',axis=1,inplace=True)df_short=df_short.drop('Unnamed:0',axis=1)deldf_short['Unnamed:0']查看數(shù)據(jù)集中各列數(shù)據(jù)的數(shù)據(jù)類(lèi)型df_short.dtypes查看是否存在重復(fù)記錄查看重復(fù)記錄數(shù)量的代碼如下:print('數(shù)據(jù)集中含有%d行重復(fù)數(shù)據(jù)'%(df_short.duplicated().sum()))查看重復(fù)記錄內(nèi)容的代碼如下:df_short[df_short.duplicated()]查看客戶ID為“587314”的購(gòu)買(mǎi)記錄,代碼如下:df_short[df_short.customer_id=='587314']京東客戶消費(fèi)數(shù)據(jù)預(yù)處理與整體消費(fèi)特征分析查看數(shù)據(jù)集行數(shù)df_short['customer_id'].count()查看有無(wú)缺失數(shù)據(jù)df_short.isnull().sum()輸出結(jié)果:55148查看數(shù)據(jù)集中各列數(shù)據(jù)去重后的數(shù)量print('數(shù)據(jù)集中“customer_id”列去重后的數(shù)據(jù)數(shù)量為:',df_short['customer_id'].nunique())print('數(shù)據(jù)集中“product_id”列去重后的數(shù)據(jù)數(shù)量為:',df_short['product_id'].nunique())print('數(shù)據(jù)集中“type”列去重后的數(shù)據(jù)數(shù)量為:',df_short['type'].nunique())print('數(shù)據(jù)集中“brand”列去重后的數(shù)據(jù)數(shù)量為:',df_short['brand'].nunique())print('數(shù)據(jù)集中“category”列去重后的數(shù)據(jù)數(shù)量為:',df_short['category'].nunique())print('數(shù)據(jù)集中“shop_category”列去重后的數(shù)據(jù)數(shù)量為:',df_short['shop_category'].nunique())print('數(shù)據(jù)集中“date”列去重后的數(shù)據(jù)數(shù)量為:',df_short['date'].nunique())京東客戶消費(fèi)數(shù)據(jù)預(yù)處理與整體消費(fèi)特征分析len(df_short['customer_id'].unique())df_short['customer_id'].unique().shape[0]輸出結(jié)果:49003查看無(wú)重復(fù)數(shù)據(jù)的部分內(nèi)容print(df_short['type'].unique())print('-'*75)print(df_short['brand'].unique()[0:10])print('-'*75)print(df_short['category'].unique()[0:10])print('-'*75)print(df_short['shop_category'].unique())以下代碼也能實(shí)現(xiàn)查看“customer_id”列去重后的數(shù)據(jù)數(shù)量的功能:京東客戶消費(fèi)數(shù)據(jù)預(yù)處理與整體消費(fèi)特征分析提取時(shí)間數(shù)據(jù)df_short['datetime']=pd.to_datetime(df_short['date'])df_short['year']=df_short['datetime'].dt.year.astype('int')df_short['month']=df_short['datetime'].dt.month.astype('int')df_short['weekday']=df_short['datetime'].dt.weekday.astype('int')#Monday是0df_short['day']=df_short['datetime'].dt.day.astype('int')#df_short.drop(['date'],axis=1,inplace=True)查看時(shí)間數(shù)據(jù)print('年份:')print(df_short['year'].value_counts())print('月份:')print(df_short['month'].value_counts())print('星期:')print(df_short['weekday'].value_counts())print('總天數(shù):')print(len(df_short['datetime'].unique()))print('時(shí)間數(shù)據(jù)特征:')print(df_short['datetime'].describe(datetime_is_numeric=True))京東客戶消費(fèi)數(shù)據(jù)預(yù)處理與整體消費(fèi)特征分析4.針對(duì)數(shù)據(jù)集df_short的數(shù)據(jù)預(yù)處理刪除重復(fù)行數(shù)據(jù)df_short=df_short.drop_duplicates(subset=None,keep='first',inplace=False)df_short.shape()刪除全為空的數(shù)據(jù)df_short=df_short.dropna(how='all')填充缺失數(shù)據(jù)df_short=df_short.fillna(np.nan)轉(zhuǎn)換數(shù)據(jù)類(lèi)型df_short['date']=pd.to_datetime(df_short['date'])df_short['date']=df_short.date.apply(lambdax:pd.to_datetime(x))輸出結(jié)果:(55138,7)京東客戶消費(fèi)數(shù)據(jù)預(yù)處理與整體消費(fèi)特征分析5.查看數(shù)據(jù)集df_label的相關(guān)信息查看數(shù)據(jù)集df_label的基本信息df_()掃描二維碼在線瀏覽電子活頁(yè)8-1查看代碼的輸出結(jié)果。查看數(shù)據(jù)集df_label的大小print('數(shù)據(jù)集大小:')print(df_label.shape)查看數(shù)據(jù)集df_label的行數(shù)df_label.shape[0]以下代碼也可以查看數(shù)據(jù)集的行數(shù):len(df_label['customer_id'])查看數(shù)據(jù)集df_label各列數(shù)據(jù)的數(shù)據(jù)類(lèi)型print('數(shù)據(jù)類(lèi)型:')df_label.dtypes輸出結(jié)果:

數(shù)據(jù)集大?。?49003,21)輸出結(jié)果:

49003京東客戶消費(fèi)數(shù)據(jù)預(yù)處理與整體消費(fèi)特征分析查看數(shù)據(jù)集df_label的基本統(tǒng)計(jì)情況df_label.describe()查看數(shù)據(jù)集df_label是否存在重復(fù)數(shù)據(jù)print('數(shù)據(jù)重復(fù):')df_label.duplicated().sum()查看數(shù)據(jù)集df_label有無(wú)缺失數(shù)據(jù)df_label.isnull().sum()查看數(shù)據(jù)集df_label中缺失數(shù)據(jù)的占比allNum=df_label['customer_id'].count()(df_label.isnull().sum()/allNum*100).round(2)輸出結(jié)果:數(shù)據(jù)重復(fù):0查看購(gòu)買(mǎi)的時(shí)間間隔df_label['interval_buy'].value_counts()京東客戶消費(fèi)數(shù)據(jù)預(yù)處理與整體消費(fèi)特征分析取反查看df_label[~df_label["interval_buy"].isnull()]檢查數(shù)據(jù)邏輯錯(cuò)誤與評(píng)估數(shù)據(jù)質(zhì)量#檢查數(shù)據(jù)邏輯錯(cuò)誤#未購(gòu)買(mǎi)無(wú)下單時(shí)間,符合邏輯df_label[df_label['buy_again']=='未購(gòu)買(mǎi)']['time_order'].value_counts()#“browse_not_buy”列中的“否”與“buy_again”列中的“未購(gòu)買(mǎi)”矛盾,邏輯錯(cuò)誤df_label[df_label['buy_again']=='未購(gòu)買(mǎi)']['browse_not_buy'].value_counts()#“cart_not_buy”列中的“否”與“buy_again”列中的“未購(gòu)買(mǎi)”矛盾,邏輯錯(cuò)誤df_label[df_label['buy_again']=='未購(gòu)買(mǎi)']['cart_not_buy'].value_counts()#判斷buy_again是否合理,找出下單時(shí)間為空但是再次購(gòu)買(mǎi)的矛盾數(shù)據(jù)contradict=df_label[(df_label['buy_again']=='是')&\(df_label['interval_buy']!=df_label['interval_buy'])]京東客戶消費(fèi)數(shù)據(jù)預(yù)處理與整體消費(fèi)特征分析6.針對(duì)數(shù)據(jù)集df_label的數(shù)據(jù)預(yù)處理填充缺失數(shù)據(jù)將none、nan用nan替換df_label=df_label.fillna(np.nan)#對(duì)空值進(jìn)行填充df_label[['time_browse','time_order','cate_most_browse','cate_most_order']]=df_label[['time_browse','time_order','cate_most_browse','cate_most_order']].fillna('NoRec')df_label[['month_buy','month_cart','month_active','week_buy','week_cart','week_active','last_order','last_browse']]=df_label[['month_buy','month_cart','month_active','week_buy','week_cart','week_active','last_order','last_browse']].fillna(0)#用0補(bǔ)齊部分缺失數(shù)據(jù),表示客戶沒(méi)有該行為foriin['month_buy','month_cart','month_active','week_buy','week_cart','week_active']京東客戶消費(fèi)數(shù)據(jù)預(yù)處理與整體消費(fèi)特征分析刪除缺失值較多的列df_label=df_label.drop('Unnamed:0',axis=1)df_label.drop(columns=['cate_most_follow','cate_most_savedCart','last_savedCart'],inplace=False)df_label[df_label['buy_again']=='是'].dropna(axis=1)刪除重復(fù)數(shù)據(jù)df_label=df_label.drop_duplicates(subset=None,keep='first',inplace=False)刪除全為空的數(shù)據(jù)df_label=df_label.dropna(how='all')df_label.dropna(axis=0,subset=['cate_most_browse'],how='all',inplace=True)刪除異常數(shù)據(jù)#刪除異常數(shù)據(jù)delete_index=contradict.index.values.tolist()df_label=df_label.drop(delete_index,axis='index')京東客戶消費(fèi)數(shù)據(jù)預(yù)處理與整體消費(fèi)特征分析改正邏輯錯(cuò)誤數(shù)據(jù)#改正邏輯錯(cuò)誤數(shù)據(jù)df_label['browse_not_buy']=np.where(df_label['buy_again']=='未購(gòu)買(mǎi)','是','否')df_label['cart_not_buy']=np.where(df_label['buy_again']=='未購(gòu)買(mǎi)','是','否')輸出結(jié)果:array(['未購(gòu)買(mǎi)','否','是'],dtype=object)df_label['buy_again'].value_counts()查看客戶復(fù)購(gòu)情況#buy_again用于表示復(fù)購(gòu)#“未購(gòu)買(mǎi)”指客戶從未購(gòu)買(mǎi),“否”指客戶未復(fù)購(gòu),“是”指客戶復(fù)購(gòu)pd.unique(df_label["buy_again"].values)輸出結(jié)果:未購(gòu)買(mǎi)45826否3165是7Name:buy_again,dtype:int64京東客戶消費(fèi)數(shù)據(jù)預(yù)處理與整體消費(fèi)特征分析7.計(jì)算客戶統(tǒng)計(jì)總數(shù)量讀取數(shù)據(jù)path=r'data\df_short_buy.xlsx'short_buy_df=pd.read_excel(path)轉(zhuǎn)換數(shù)據(jù)類(lèi)型與提取日期信息short_buy_df['date']=pd.to_datetime(short_buy_df['date'])short_buy_df['date'].max(),short_buy_df['date'].min()提取時(shí)間數(shù)據(jù)short_buy_df['年']=short_buy_df['date'].dt.yearshort_buy_df['月']=short_buy_df['date'].dt.monthshort_buy_df['日']=short_buy_df['date'].dt.dayshort_buy_df['周']=short_buy_df['date'].dt.weekday京東客戶消費(fèi)數(shù)據(jù)預(yù)處理與整體消費(fèi)特征分析刪除數(shù)據(jù)集中多余列與查看數(shù)據(jù)集前5行數(shù)據(jù)short_buy_df=short_buy_df.drop('Unnamed:0',axis=1)short_buy_df.head()按日期列排序并重置索引short_buy_df.sort_values(by='date',ascending=True,inplace=True)short_buy_df.reset_index(drop=True,inplace=True)輸出客戶統(tǒng)計(jì)總數(shù)量print('客戶統(tǒng)計(jì)總數(shù)量:',short_buy_df['統(tǒng)計(jì)數(shù)量'].sum(),'件')輸出結(jié)果:客戶統(tǒng)計(jì)總數(shù)量:55138件京東客戶消費(fèi)數(shù)據(jù)預(yù)處理與整體消費(fèi)特征分析8.計(jì)算客戶消費(fèi)主要電商指標(biāo)將表示客戶行為類(lèi)型的數(shù)字轉(zhuǎn)換為字符action_map={1:'瀏覽',2:'評(píng)論',3:'購(gòu)買(mǎi)',4:'收藏',5:'加入購(gòu)物車(chē)'}df_short['type']=df_short['type'].map(action_map)df_short.head()京東客戶消費(fèi)數(shù)據(jù)預(yù)處理與整體消費(fèi)特征分析計(jì)算總訪問(wèn)量、日均訪問(wèn)量、總訪客數(shù)和人均訪問(wèn)量等pv=df_short[df_short['type']=='瀏覽']['customer_id'].count()#總訪問(wèn)量uv=len(df_short['customer_id'].unique())#總訪客數(shù)pv_per_user=pv/uv#人均訪問(wèn)量pv_per_day=pv/len(df_short['date'])#日均訪問(wèn)量print('總訪問(wèn)量為:%i'%pv)print('日均訪問(wèn)量為:%.3f'%pv_per_day)print('總訪客數(shù)為:%i'%uv)print('人均訪問(wèn)量為:%.3f'%pv_per_user)輸出結(jié)果:總訪問(wèn)量為:49139日均訪問(wèn)量為:0.891總訪客數(shù)為:49003人均訪問(wèn)量為:1.003京東客戶消費(fèi)數(shù)據(jù)預(yù)處理與整體消費(fèi)特征分析計(jì)算消費(fèi)客戶數(shù)、消費(fèi)客戶訪問(wèn)量、消費(fèi)客戶人均訪問(wèn)量和客戶跳失率等user_pay=df_short[df_short['type']=='購(gòu)買(mǎi)']['customer_id'].unique()#消費(fèi)客戶數(shù)user_pay_rate=len(user_pay)/uv#消費(fèi)客戶數(shù)占比#消費(fèi)客戶訪問(wèn)量pv_pay=df_short[df_short['customer_id'].isin(user_pay)]['type'].value_counts()['瀏覽']pv_pay_rate=pv_pay/pv#消費(fèi)客戶訪問(wèn)量占比pv_per_buy_user=pv_pay/len(user_pay)#消費(fèi)客戶人均訪問(wèn)量leave_rate=sum(df_short.groupby('customer_id')['type'].count()==1)/(df_short['customer_id'].nunique())print('消費(fèi)客戶數(shù)為:%i'%len(user_pay))print('消費(fèi)客戶數(shù)占比為:%.3f%%'%(user_pay_rate*100))print('消費(fèi)客戶訪問(wèn)量為:%i'%pv_pay)print('消費(fèi)客戶訪問(wèn)量占比為:%.3f%%'%(pv_pay_rate*100))print('消費(fèi)客戶人均訪問(wèn)量為:%.3f'%pv_per_buy_user)print('客戶跳失率為:%.3f%%'%(leave_rate*100))輸出

結(jié)果:消費(fèi)客戶數(shù)為:933消費(fèi)客戶數(shù)占比為:1.904%消費(fèi)客戶訪問(wèn)量為:199消費(fèi)客戶訪問(wèn)量占比為:0.405%消費(fèi)客戶人均訪問(wèn)量為:0.213客戶跳失率為:91.415%京東客戶消費(fèi)數(shù)據(jù)預(yù)處理與整體消費(fèi)特征分析針對(duì)數(shù)據(jù)集df_short查看購(gòu)買(mǎi)次數(shù)為1次以上的客戶#客戶購(gòu)買(mǎi)次數(shù)sku_num=(df_short[df_short['type']=='購(gòu)買(mǎi)'].groupby('customer_id')['type'].count().to_frame().rename(columns={'type':'購(gòu)買(mǎi)次數(shù)小計(jì)'}).reset_index())#購(gòu)買(mǎi)次數(shù)為1次以上的客戶topsku=sku_num[sku_num['購(gòu)買(mǎi)次數(shù)小計(jì)']>1].sort_values(by='customer_id',ascending=False)num=topsku['購(gòu)買(mǎi)次數(shù)小計(jì)'].count()print('購(gòu)買(mǎi)次數(shù)為1次以上的客戶的數(shù)量為:',num)topsku.head(num)京東客戶消費(fèi)數(shù)據(jù)預(yù)處理與整體消費(fèi)特征分析針對(duì)數(shù)據(jù)集df_consume查看有復(fù)購(gòu)行為的客戶df_consume=pd.read_excel(r'.\data\df_consume.xlsx',dtype={'customer_id':str})vip1=df_consume.loc[df_consume['buy_again']=='是']

.sort_values(by='customer_id',ascending=False)num=df_consume[df_consume['buy_again']=='是']['buy_again'].count()print('有復(fù)購(gòu)行為的客戶的數(shù)量為:',num)vip1[['customer_id','buy_again']]統(tǒng)計(jì)不同購(gòu)買(mǎi)次數(shù)的客戶的人數(shù)total_buy_count=(df_short[df_short['type']=='購(gòu)買(mǎi)'].groupby(['customer_id'])['type'].ount().to_frame().rename(columns={'type':'購(gòu)買(mǎi)次數(shù)小計(jì)'}))total_buy_count['購(gòu)買(mǎi)次數(shù)小計(jì)'].value_counts()輸出結(jié)果:京東客戶消費(fèi)數(shù)據(jù)預(yù)處理與整體消費(fèi)特征分析計(jì)算復(fù)購(gòu)率re_buy_rate=total_buy_count[total_buy_count['購(gòu)買(mǎi)次數(shù)小計(jì)']>=2].count()/total_buy_count.count()print('復(fù)購(gòu)率為:%.2f%%'%(re_buy_rate*100))輸出結(jié)果:平臺(tái)的復(fù)購(gòu)率較低。由于時(shí)間跨度較小,消費(fèi)者短期內(nèi)再次購(gòu)買(mǎi)的動(dòng)機(jī)不強(qiáng),這符合客觀規(guī)律。平臺(tái)應(yīng)該將營(yíng)銷(xiāo)重點(diǎn)放在沒(méi)有購(gòu)買(mǎi)的消費(fèi)者身上,這樣不僅能節(jié)約資源,也能提高平臺(tái)的客戶轉(zhuǎn)化率。京東客戶消費(fèi)數(shù)據(jù)預(yù)處理與整體消費(fèi)特征分析9.統(tǒng)計(jì)客戶瀏覽數(shù)量與購(gòu)買(mǎi)數(shù)量統(tǒng)計(jì)各個(gè)客戶的瀏覽數(shù)量#客戶的瀏覽數(shù)量sku_num=(short_buy_df[short_buy_df['type']==1].groupby('customer_id')['type'].count().to_frame().rename(columns={'type':'total'}).reset_index())#瀏覽數(shù)量排前5位的客戶topsku=sku_num.sort_values(by='total',ascending=False)topsku.head()統(tǒng)計(jì)各個(gè)客戶的購(gòu)買(mǎi)數(shù)量#客戶的購(gòu)買(mǎi)數(shù)量sku_num=(short_buy_df[short_buy_df['type']==3].groupby('customer_id')['type'].count().to_frame().rename(columns={'type':'total'}).reset_index())#購(gòu)買(mǎi)數(shù)量排前5位的客戶topsku=sku_num.sort_values(by='total',ascending=False)topsku.head()【任務(wù)描述】現(xiàn)有3個(gè)Excel文件,分別是“df_product.xlsx”“df_product_buy.xlsx”和“df_consume.xlsx”,“df_product.xlsx”文件中有8列、55148行數(shù)據(jù),“df_consume.xlsx”文件中有21列、49003行數(shù)據(jù),Excel文件“df_product.xlsx”的主要列名稱(chēng)及說(shuō)明可參見(jiàn)表8-1,Excel文件“df_consume.xlsx”的主要列名稱(chēng)及說(shuō)明可參見(jiàn)表8-2。針對(duì)df_product、df_product_buy和df_consume3個(gè)數(shù)據(jù)集完成以下操作。針對(duì)數(shù)據(jù)集df_product分析京東電商客戶喜好的商品大類(lèi)。針對(duì)數(shù)據(jù)集df_product分析京東電商客戶喜好的商品小類(lèi)。數(shù)據(jù)集df_product_buy中增加了“購(gòu)買(mǎi)數(shù)量”列,針對(duì)數(shù)據(jù)集df_product_buy分析京東電商客戶喜好的商品小類(lèi)。針對(duì)數(shù)據(jù)集df_consume分析京東電商客戶喜好的商品小類(lèi)?!救蝿?wù)實(shí)現(xiàn)】【任務(wù)8-2】京東電商客戶喜好的商品大類(lèi)及細(xì)分類(lèi)型分析京東電商客戶喜好的商品大類(lèi)及細(xì)分類(lèi)型分析1.針對(duì)數(shù)據(jù)集df_product分析京東電商客戶喜好的商品大類(lèi)導(dǎo)入模塊importmathimportplotlyaspyimportmatplotlibasmplimportmatplotlib.gridspecasgridspecimportplotly.graph_objsasgofromplotly.graph_objsimportScatterfrompyecharts.globalsimportThemeTypefrompyecharts.fakerimportFakerfromdatetimeimportdatetimesns.set(font='FangSong')pyplot=py.offline.iplot讀取數(shù)據(jù)與預(yù)處理path=r'data\df_product.xlsx'data1=pd.read_excel(path,parse_dates=['date'],dtype={'customer_id':str,'product_id':str})data1=data1.drop('Unnamed:0',axis=1)df_short=data1.copy()京東電商客戶喜好的商品大類(lèi)及細(xì)分類(lèi)型分析查看商品大類(lèi)與統(tǒng)計(jì)商品大類(lèi)已購(gòu)商品的數(shù)量查看商品大類(lèi)的代碼如下:df_short.shop_category.unique()統(tǒng)計(jì)每一個(gè)商品大類(lèi)已購(gòu)商品的數(shù)量的代碼如下:統(tǒng)計(jì)每一個(gè)商品大類(lèi)已購(gòu)商品的數(shù)量的代碼如下:shopCategoryCounts=df_short.shop_category.value_counts()#shop_category=data_short['shop_category'].value_counts()shopCategoryCounts繪制餅圖分析京東電商客戶喜好的商品大類(lèi)方法1的代碼如下:輸出結(jié)果:array(['Electronics','Clothes','HouseholdElectricAppliance','MotherandInfant','Food','BeautyMakeup','OutdoorSports','Furniture','JewelleryAccessories'],dtype=object)trace=[go.Pie(labels=shopCategoryCounts.index.tolist(),values=shopCategoryCounts.values.tolist(),textfont=dict(size=12,color='white'))]layout=go.Layout(title='商品大類(lèi)占比')fig=go.Figure(data=trace,layout=layout)pyplot(fig)京東電商客戶喜好的商品大類(lèi)及細(xì)分類(lèi)型分析京東電商客戶喜好的商品大類(lèi)及細(xì)分類(lèi)型分析掃描二維碼在線瀏覽電子活頁(yè)8-2“繪制餅圖分析京東電商客戶喜好的商品大類(lèi)方法2”中的代碼及繪制的圖形。掃描二維碼在線瀏覽電子活頁(yè)8-3“繪制餅圖分析京東電商客戶喜好的商品大類(lèi)方法3”中的代碼及繪制的圖形。掃描二維碼在線瀏覽電子活頁(yè)8-4“繪制餅圖分析京東電商客戶喜好的商品大類(lèi)方法4”中的代碼及繪制的圖形。繪制餅圖分析特定的商品大類(lèi)、商品小類(lèi)與商品品牌的統(tǒng)計(jì)數(shù)量electronicsData=df_short[df_short['shop_category']=='Electronics']phoneCategoryCounts=electronicsData.category.value_counts()data_pair_categoryCounts=[list(i)foriinzip(phoneCategoryCounts.keys(),phoneCategoryCounts.values.tolist())]京東電商客戶喜好的商品大類(lèi)及細(xì)分類(lèi)型分析#繪制餅圖phoneCategoryCountsPie=(Pie().add("商品小類(lèi)",data_pair_categoryCounts).set_global_opts(title_opts=opts.TitleOpts(title="Electronics大類(lèi)的各細(xì)分種類(lèi)的統(tǒng)計(jì)數(shù)量分布",subtitle='2022/02/01-2022/04/15'),tooltip_opts=opts.TooltipOpts(trigger="axis",is_show=True,trigger_on="click",axis_pointer_type="cross",border_color="#FF0000",border_width=5),legend_opts=opts.LegendOpts(type_="plain",is_show=True,orient="vertical",pos_left='right'),).set_series_opts(label_opts=opts.LabelOpts(formatter=":{c}")).render_notebook())phoneCategoryCountsPie京東電商客戶喜好的商品大類(lèi)及細(xì)分類(lèi)型分析掃描二維碼在線瀏覽電子活頁(yè)8-5“針對(duì)特定商品大類(lèi)Clothes分析其細(xì)分種類(lèi)的分布”中的代碼及繪制的圖形。京東電商客戶喜好的商品大類(lèi)及細(xì)分類(lèi)型分析df_1=df_short.groupby('shop_category')['customer_id'].count().reset_index().sort_values('customer_id',ascending=False).reset_index(drop=True)num=df_1['customer_id'].valueslabels=[xforxindf_1['shop_category']]explode=[0.02,0.01,0,0,0,0,0,0,0]plt.figure(figsize=(12,8))plt.pie(num,labels=labels,explode=explode,autopct='%.2f%%',pctdistance=0.8,labeldistance=1.15,startangle=180,textprops={'fontsize':12})plt.title('各商品大類(lèi)的統(tǒng)計(jì)數(shù)量的占比')plt.show()首先繪制各商品大類(lèi)統(tǒng)計(jì)數(shù)量占比餅圖,代碼如下:京東電商客戶喜好的商品大類(lèi)及細(xì)分類(lèi)型分析掃描二維碼在線瀏覽電子活頁(yè)8-6“分析Electronics大類(lèi)中各商品小類(lèi)的統(tǒng)計(jì)數(shù)量分布情況”中的代碼及繪制的圖形。分析Phone小類(lèi)中受歡迎的品牌的統(tǒng)計(jì)數(shù)量分布情況,其代碼如下:phone=electronics.loc[electronics['category'].str.contains('Phone')]df_3=phone.groupby('brand')['customer_id'].count().reset_index().sort_values('customer_id',ascending=False).reset_index(drop=True)num=df_3['customer_id'].valueslabels=[xforxindf_3['brand']]explode=[0.02,0.01,0,0,0,0,0]plt.subplots(figsize=(12,8))plt.pie(num,labels=labels,explode=explode,autopct='%.2f%%',pctdistance=0.8,labeldistance=1.15,startangle=180,textprops={'fontsize':12})plt.title('受歡迎手機(jī)品牌的統(tǒng)計(jì)數(shù)量分布')plt.show()京東電商客戶喜好的商品大類(lèi)及細(xì)分類(lèi)型分析京東電商客戶喜好的商品大類(lèi)及細(xì)分類(lèi)型分析繪制餅圖分析HouseholdElectricAppliance大類(lèi)的商品小類(lèi)、商品品牌、分月統(tǒng)計(jì)、每周按天統(tǒng)計(jì)的數(shù)量分布df_short["month"]=df_short["date"].dt.monthdf_short["weekday"]=df_short["date"].dt.weekday#定義分析函數(shù)shop_cate()defshop_cate(category):data_1=df_short[df_short['shop_category']==category]print(category+'類(lèi)的統(tǒng)計(jì)數(shù)量:',data_1.shape[0])plt.figure(figsize=(16,10))plt.subplot(2,2,1)data_1['category'].value_counts().plot(kind='pie',autopct='%.1f%%',label='')plt.subplot(2,2,2)data_1['brand'].value_counts().plot(kind='pie',autopct='%.1f%%',label='')plt.subplot(2,2,3)data_1['month'].value_counts().plot(kind='pie',autopct='%.1f%%',label='')plt.subplot(2,2,4)data_1['weekday'].value_counts().plot(kind='pie',autopct='%.1f%%',label='')plt.show()#家用電器類(lèi)shop_cate('HouseholdElectricAppliance')京東電商客戶喜好的商品大類(lèi)及細(xì)分類(lèi)型分析京東電商客戶喜好的商品大類(lèi)及細(xì)分類(lèi)型分析繪制條形圖分析京東電商客戶喜好的商品大類(lèi)df=df_short['shop_category'].value_counts().to_frame()x_name=df.indexy_count=df.shop_categoryplt.figure(figsize=(10,6))sns.barplot(y=x_name,x=y_count.values).set(title='商品大類(lèi)的統(tǒng)計(jì)數(shù)量分布')plt.show()京東電商客戶喜好的商品大類(lèi)及細(xì)分類(lèi)型分析繪制柱形圖分析京東電商客戶喜好的商品大類(lèi)方法1的代碼如下:trace_basic=go.Bar(x=x_name.tolist(),y=y_count.values.tolist())layout=go.Layout(title='商品大類(lèi)的受歡迎程度')figure_basic=go.Figure(data=trace_basic,layout=layout)pyplot(figure_basic)京東電商客戶喜好的商品大類(lèi)及細(xì)分類(lèi)型分析掃描二維碼在線瀏覽電子活頁(yè)8-7“繪制柱形圖分析京東電商客戶喜好的商品大類(lèi)方法2”中的代碼及繪制的圖形。掃描二維碼在線瀏覽電子活頁(yè)8-8“繪制柱形圖分析京東電商客戶喜好的商品大類(lèi)方法3”中的代碼及繪制的圖形。掃描二維碼在線瀏覽電子活頁(yè)8-9“繪制柱形圖分析京東電商客戶喜好的商品大類(lèi)方法4”中的代碼及繪制的圖形。繪制柱形圖分析Electronics大類(lèi)中的Phone小類(lèi)的品牌分布情況phoneData=df_short.query('category=="Phone"andshop_category=="Electronics"')brandCounts=phoneData.brand.value_counts()京東電商客戶喜好的商品大類(lèi)及細(xì)分類(lèi)型分析brandCountsBar=(Bar().add_xaxis(brandCounts.keys().tolist()).add_yaxis('統(tǒng)計(jì)數(shù)量',brandCounts.values.tolist()).set_global_opts(title_opts=opts.TitleOpts(title="各品牌手機(jī)的統(tǒng)計(jì)數(shù)量分布",subtitle='2022/02/01-2022/04/15'),tooltip_opts=opts.TooltipOpts(trigger="axis",is_show=True,trigger_on="click",axis_pointer_type="cross",border_color="#FF0000",border_width=5),legend_opts=opts.LegendOpts(type_="plain",is_show=True,orient="vertical",pos_left='right'),).set_series_opts(label_opts=opts.LabelOpts(formatter=":{c}")).render_notebook())brandCountsBar京東電商客戶喜好的商品大類(lèi)及細(xì)分類(lèi)型分析京東電商客戶喜好的商品大類(lèi)及細(xì)分類(lèi)型分析繪制矩形樹(shù)形圖分析京東電商客戶喜好的各商品大類(lèi)產(chǎn)品的購(gòu)買(mǎi)情況path=r'data\df_product_buy.xlsx'df_product_buy=pd.read_excel(path)df=df_product_buy.groupby(['shop_category','category','brand']).agg({'購(gòu)買(mǎi)數(shù)量':'sum'})df.reset_index(inplace=True)tdata=[]foriindf.shop_category.unique():df_child1=[]forjindf[(df['shop_category']==i)].category.unique():df_child2=[]forkinrange(0,df[(df['shop_category']==i)&(df['category']==j)].shape[0]):df_child2.append({"value":df[(df['shop_category']==i)&(df['category']==j)].購(gòu)買(mǎi)數(shù)量.tolist()[k],"name":df[(df['shop_category']==i)&(df['category']==j)].brand.tolist()[k]})京東電商客戶喜好的商品大類(lèi)及細(xì)分類(lèi)型分析df_child1.append({"value":df[(df['shop_category']==i)&(df['category']==j)].購(gòu)買(mǎi)數(shù)量.sum(),"name":j,"children":df_child2,})tdata.append({"value":df[df['shop_category']==i].購(gòu)買(mǎi)數(shù)量.sum(),"name":i,"children":df_child1,})treemap=(TreeMap(init_opts=opts.InitOpts(width="900px",height="600px")).add(series_name="商品大類(lèi)",data=tdata,leaf_depth=1,label_opts=opts.LabelOpts(position="inside",formatter=':{c}'),).set_global_opts(title_opts=opts.TitleOpts(title="各商品大類(lèi)產(chǎn)品的購(gòu)買(mǎi)情況"),))treemap.render_notebook()京東電商客戶喜好的商品大類(lèi)及細(xì)分類(lèi)型分析從圖8-9可以看出:共有9種一級(jí)分類(lèi),即9個(gè)商品大類(lèi),其中Electronics大類(lèi)的購(gòu)買(mǎi)數(shù)量遠(yuǎn)超其他大類(lèi)的。繪制自定義樣式柱形圖分析京東電商客戶喜好的商品大類(lèi)京東電商客戶喜好的商品大類(lèi)及細(xì)分類(lèi)型分析2.針對(duì)數(shù)據(jù)集df_product分析京東電商客戶喜好的商品小類(lèi)導(dǎo)入模塊導(dǎo)入通用模塊的代碼詳見(jiàn)“本書(shū)導(dǎo)學(xué)”,其他模塊導(dǎo)入的代碼詳見(jiàn)“1.針對(duì)數(shù)據(jù)集df_product分析京東電商客戶喜好的商品大類(lèi)”。讀取數(shù)據(jù)與預(yù)處理path=r'data\df_product.xlsx'data1=pd.read_excel(path,parse_dates=['date'],dtype={'customer_id':str,'product_id':str})data1=data1.drop('Unnamed:0',axis=1)df_short=data1在JupyterNotebook開(kāi)發(fā)環(huán)境中創(chuàng)建tc08-02-02.ipynb,然后在單元格中編寫(xiě)代碼并輸出對(duì)應(yīng)的結(jié)果。京東電商客戶喜好的商品大類(lèi)及細(xì)分類(lèi)型分析查看商品小類(lèi)與統(tǒng)計(jì)商品小類(lèi)的數(shù)量category10=df_short['category'].value_counts().head(10)category10繪制餅圖分析京東電商客戶喜好的商品小類(lèi)方法1的代碼如下:pyplot=py.offline.iplottrace=[go.Pie(labels=category10.index.tolist(),values=category10.values.tolist(),textfont=dict(size=12,color='white'))]layout=go.Layout(title='統(tǒng)計(jì)數(shù)量排前10位的商品小類(lèi)的數(shù)量占比情況')fig=go.Figure(data=trace,layout=layout)pyplot(fig)京東電商客戶喜好的商品大類(lèi)及細(xì)分類(lèi)型分析掃描二維碼在線瀏覽電子活頁(yè)8-10“繪制餅圖分析京東電商客戶喜好的商品小類(lèi)方法2”中的代碼及繪制的圖形。繪制條形圖分析京東電商客戶喜好的商品小類(lèi)plt.figure(figsize=(10,6))sns.barplot(y=category10.index,x=category10.values).set(title='統(tǒng)計(jì)數(shù)量排前10位的商品小類(lèi)')plt.show()京東電商客戶喜好的商品大類(lèi)及細(xì)分類(lèi)型分析繪制柱形圖分析京東電商客戶喜好的商品小類(lèi)plt.figure(figsize=(10,6),dpi=80)plt.bar(x=category10.index,height=category10.values,width=0.5)forx,yinzip(category10.index,category10.values):plt.text(x,y,'{0}'.format(y),ha='center',va='bottom')plt.xlabel('產(chǎn)品小類(lèi)')plt.ylabel('統(tǒng)計(jì)數(shù)量')plt.title('統(tǒng)計(jì)數(shù)量排前10位的商品小類(lèi)')plt.xticks(rotation=30)plt.show()方法1的代碼如下:京東電商客戶喜好的商品大類(lèi)及細(xì)分類(lèi)型分析繪制棒棒糖圖分析京東電商客戶喜好的商品小類(lèi)掃描二維碼在線瀏覽電子活頁(yè)8-11“繪制柱形圖分析京東電商客戶喜好的商品小類(lèi)方法2”中的代碼及繪制的圖形。掃描二維碼在線瀏覽電子活頁(yè)8-12“繪制柱形圖分析京東電商客戶喜好的商品小類(lèi)方法3”中的代碼及繪制的圖形。掃描二維碼在線瀏覽電子活頁(yè)8-13“繪制柱形圖分析京東電商客戶喜好的商品小類(lèi)方法4”中的代碼及繪制的圖形。掃描二維碼在線瀏覽電子活頁(yè)8-14“繪制棒棒糖圖分析京東電商客戶喜好的商品小類(lèi)”中的代碼及繪制的圖形。京東電商客戶喜好的商品大類(lèi)及細(xì)分類(lèi)型分析繪制商品類(lèi)型名稱(chēng)詞云圖cloud1_name=df_short.groupby(['category']).count().sort_values(by='customer_id',ascending=False)['customer_id'].index.to_list()cloud1_num=df_short.groupby(['category']).count().sort_values(by='customer_id',ascending=False)['customer_id'].to_list()cloud1=WordCloud(init_opts=opts.InitOpts(width='600px',height='400px'))cloud1.add('',[list(z)forzinzip(cloud1_name,cloud1_num)],word_size_range=[10,80])cloud1.set_global_opts(title_opts=

opts.TitleOpts(title="商品類(lèi)型名稱(chēng)詞云",pos_left='center',pos_top='2%'))cloud1.render_notebook()京東電商客戶喜好的商品大類(lèi)及細(xì)分類(lèi)型分析3.針對(duì)數(shù)據(jù)集df_product_buy分析京東電商客戶喜好的商品小類(lèi)讀取數(shù)據(jù)與預(yù)處理path=r'data\df_product_buy.xlsx'df_product_buy=pd.read_excel(path)df_product_buy=df_product_buy.drop('Unnamed:0',axis=1)df_product_buy.head()查看數(shù)據(jù)集中所有商品小類(lèi)的總購(gòu)買(mǎi)數(shù)量df_product_buy['購(gòu)買(mǎi)數(shù)量'].sum()輸出結(jié)果:945京東電商客戶喜好的商品大類(lèi)及細(xì)分類(lèi)型分析統(tǒng)計(jì)購(gòu)買(mǎi)數(shù)量排前10位的商品小類(lèi)的購(gòu)買(mǎi)數(shù)量cate_sale=df_product_buy.groupby('category').agg({'購(gòu)買(mǎi)數(shù)量':'sum'})cate_sale.rename(columns={'購(gòu)買(mǎi)數(shù)量':'購(gòu)買(mǎi)數(shù)量'},inplace=True)cate_sale.sort_values(by='購(gòu)買(mǎi)數(shù)量',inplace=True,ascending=False)cate_sale.reset_index(inplace=True)cate_sale=cate_sale.head(10)cate_sale查看數(shù)據(jù)集中購(gòu)買(mǎi)數(shù)量排前10位的商品小類(lèi)的總購(gòu)買(mǎi)數(shù)量cate_sale['購(gòu)買(mǎi)數(shù)量'].sum()輸出結(jié)果:610繪制自定義樣式條形圖分析購(gòu)買(mǎi)數(shù)量排前10位的商品小類(lèi)繪制自定義樣式玫瑰圖分析購(gòu)買(mǎi)數(shù)量排前10位的商品小類(lèi)京東電商客戶喜好的商品大類(lèi)及細(xì)分類(lèi)型分析4.針對(duì)數(shù)據(jù)集df_consume分析京東電商客戶喜好的商品小類(lèi)讀取數(shù)據(jù)與預(yù)處理導(dǎo)入通用模塊的代碼詳見(jiàn)“本書(shū)導(dǎo)學(xué)”,其他模塊導(dǎo)入的代碼詳見(jiàn)“1.針對(duì)數(shù)據(jù)集df_product分析京東電商客戶喜好的商品大類(lèi)”。讀取數(shù)據(jù)與預(yù)處理data2=pd.read_excel(r'.\data\df_consume.xlsx',dtype={'customer_id':str})df_label=data2.copy()cate_most=df_label[['customer_id','cate_most_browse','cate_most_follow','cate_most_savedCart','cate_most_order']].copy()most_browse=cate_most.copy()most_browse.dropna(axis=0,subset=['cate_most_browse'],how='all',inplace=True)most_browse.reset_index(drop=True,inplace=True)cate_most_browse=pd.DataFrame(cate_most[cate_most['cate_most_browse']!='NoRec']['cate_most_browse].value_counts())cate_most_browse.head()在JupyterNotebook開(kāi)發(fā)環(huán)境中創(chuàng)建tc08-02-03.ipynb,然后在單元格中編寫(xiě)代碼并輸出對(duì)應(yīng)的結(jié)果。京東電商客戶喜好的商品大類(lèi)及細(xì)分類(lèi)型分析對(duì)數(shù)據(jù)集“商品類(lèi)型”列中包含的多個(gè)類(lèi)型名稱(chēng)進(jìn)行分解cate_most_browse1=[]foriinrange(most_browse.shape[0]):browse=most_browse['cate_most_browse'].loc[i].split(',')forjinrange(len(browse)):cate=browse[j]cate_most_browse1.append(cate)cate_most_browse1=pd.DataFrame(cate_most_browse1)cate_most_browse1['瀏覽次數(shù)']=1cate_most_browse1.rename(columns={0:'商品類(lèi)型'},inplace=True)cate_most_browse1統(tǒng)計(jì)各種商品類(lèi)型的瀏覽次數(shù)cate_most_browse2=cate_most_browse1.groupby('商品類(lèi)型').count()cate_most_browse2.sort_values(by=['瀏覽次數(shù)'],ascending=False,inplace=True)京東電商客戶喜好的商品大類(lèi)及細(xì)分類(lèi)型分析獲取瀏覽次數(shù)排前10位的商品類(lèi)型方法1的代碼如下:cate_most_browse3=cate_most_browse2.head(10).copy()cate_most_browse3.reset_index(inplace=True)cate_most_browse3cate_category10=cate_most_browse1['商品類(lèi)型'].value_counts().head(10)方法2的代碼如下:cate_most_browse10=cate_most_browse2.head(10)cate_most_browse10方法3的代碼如下:京東電商客戶喜好的商品大類(lèi)及細(xì)分類(lèi)型分析繪制條形圖分析京東電商客戶瀏覽次數(shù)排前10位的商品類(lèi)型plt.figure(figsize=(10,8))sns.barplot(y=cate_category10.index,x=cate_category10.values)

.set(title='京東電商客戶瀏覽次數(shù)排前10位的商品類(lèi)型')plt.show()京東電商客戶喜好的商品大類(lèi)及細(xì)分類(lèi)型分析繪制柱形圖分析京東電商客戶瀏覽次數(shù)排前10位的商品類(lèi)型bar1=(Bar().add_xaxis(cate_most_browse3['商品類(lèi)型'].to_list()).add_yaxis('商品類(lèi)型',cate_most_browse3['瀏覽次數(shù)'].to_list()).set_global_opts(xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=-15)),

title_opts=opts.TitleOpts(title="瀏覽次數(shù)")))bar1.render_notebook()方法1的代碼如下:京東電商客戶喜好的商品大類(lèi)及細(xì)分類(lèi)型分析定義拆分?jǐn)?shù)據(jù)集“商品類(lèi)型”列中包含的多個(gè)類(lèi)型名稱(chēng)的函數(shù)掃描二維碼在線瀏覽電子活頁(yè)8-15“繪制柱形圖分析京東電商客戶瀏覽次數(shù)排前10位的商品類(lèi)型方法2”中的代碼及繪制的圖形。掃描二維碼在線瀏覽電子活頁(yè)8-16“繪制柱形圖分析京東電商客戶瀏覽次數(shù)排前10位的商品類(lèi)型方法3”中的代碼及繪制的圖形。defdecompose(cate_most_name,name):most_x=cate_most.copy()most_x.dropna(axis=0,subset=[cate_most_name],how='all',inplace=True)most_x.reset_index(drop=True,inplace=True)cate_most_type=[]foriinrange(most_x.shape[0]):category=most_x[cate_most_name].loc[i].split(',')京東電商客戶喜好的商品大類(lèi)及細(xì)分類(lèi)型分析forjinrange(len(category)):cate=category[j]cate_most_type.append(cate)cate_most_category=pd.DataFrame(cate_most_type)cate_most_category[name]=1cate_most_category.rename(columns={0:'商品類(lèi)型'},inplace=True)returncate_most_category京東電商客戶喜好的商品大類(lèi)及細(xì)分類(lèi)型分析繪制柱形圖分析京東電商客戶訂購(gòu)次數(shù)排前10位的商品類(lèi)型掃描二維碼在線瀏覽電子活頁(yè)8-17“繪制柱形圖分析京東電商客戶加入購(gòu)物車(chē)次數(shù)排前10位的商品類(lèi)型”中的代碼及繪制的圖形。掃描二維碼在線瀏覽電子活頁(yè)8-18“繪制柱形圖分析京東電商客戶加入購(gòu)物車(chē)次數(shù)排前10位的商品類(lèi)型方法2”中的代碼及繪制的圖形。cate_most_order1=decompose('cate_most_order','訂購(gòu)次數(shù)')['商品類(lèi)型'].value_counts().head(10)cate_most_order1.plot(kind='bar',color=['b','g','m'],figsize=(10,6))plt.title('京東電商客戶訂購(gòu)次數(shù)排前10位的商品類(lèi)型',size=15)plt.xticks(rotation=45)plt.show()繪制柱形圖分析京東電商客戶加入購(gòu)物車(chē)次數(shù)排前10位的商品類(lèi)型方法1的代碼如下:京東電商客戶喜好的商品大類(lèi)及細(xì)分類(lèi)型分析繪制柱形圖分析實(shí)際購(gòu)買(mǎi)數(shù)量較多的京東電商客戶actual_buy_data=df_label[df_label.time_order.isnull()==False]actual_buy_data_counts=actual_buy_data.cate_most_order.value_counts()actual_buy_data_counts=actual_buy_data_counts[actual_buy_data_counts>150]數(shù)據(jù)集中“time_order”列的值不為空,則表示實(shí)際購(gòu)買(mǎi)。獲取客戶實(shí)際購(gòu)物數(shù)據(jù)的代碼如下:京東電商客戶喜好的商品大類(lèi)及細(xì)分類(lèi)型分析actual_buy_data_countsBar=Bar()actual_buy_data_countsBar.add_xaxis(actual_buy_data_counts.keys().tolist())actual_buy_data_countsBar.add_yaxis("購(gòu)買(mǎi)數(shù)量",actual_buy_data_counts.values.tolist())actual_buy_data_countsBar.set_global_opts(title_opts=opts.TitleOpts(title="京東電商客戶購(gòu)買(mǎi)數(shù)量較多的商品"),xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=-15)),tooltip_opts=opts.TooltipOpts(trigger="axis",is_show=True,trigger_on="click",axis_pointer_type="cross",border_color="#FF0000",border_width=5),legend_opts=opts.LegendOpts(type_="plain",is_show=True,orient="vertical",pos_left='right'),)actual_buy_data_countsBar.set_series_opts(label_opts=opts.LabelOpts(formatter=":{c}"))actual_buy_data_countsBar.render_notebook()分析實(shí)際購(gòu)買(mǎi)數(shù)量較多的京東電商客戶,繪制其實(shí)際購(gòu)買(mǎi)數(shù)量柱形圖的代碼如下:京東電商客戶喜好的商品大類(lèi)及細(xì)分類(lèi)型分析在同一區(qū)域繪制多張柱形圖分析瀏覽次數(shù)、收藏次數(shù)、加入購(gòu)物車(chē)次數(shù)和訂購(gòu)次數(shù)排前5位的客戶n=5#取前5位df1=decompose('cate_most_browse','瀏覽次數(shù)')['商品類(lèi)型'].value_counts().to_frame().head(n)df1.rename(columns={'商品類(lèi)型':'瀏覽次數(shù)'},inplace=True)df2=decompose('cate_most_follow','收藏次數(shù)')['商品類(lèi)型'].value_counts().to_frame().head(n)df2.rename(columns={'商品類(lèi)型':'收藏次數(shù)'},inplace=True)df3=decompose('cate_most_savedCart','加入購(gòu)物車(chē)次數(shù)')['商品類(lèi)型'].value_counts().to_frame().head(n)df3.rename(columns={'商品類(lèi)型':'加入購(gòu)物車(chē)次數(shù)'},inplace=True)df4=decompose('cate_most_order','訂購(gòu)次數(shù)')['商品類(lèi)型'].value_counts().to_frame().head(5)df4.rename(columns={'商品類(lèi)型':'訂購(gòu)次數(shù)'},inplace=True)京東電商客戶喜好的商品大類(lèi)及細(xì)分類(lèi)型分析繪制多張柱形圖的代碼如下:plt.figure(figsize=(10,8)

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論