運用長條圖一窺顧客交易資料的商務應用 — 行銷活動回購分析(附Python程式碼)

分析
Photo by: fauxels

一、導入套件包與資料集

# 套件包import pandas as pdimport numpy as npfrom datetime import timedelta, datetime, tzinfo, timezoneimport plotly.express as pximport plotly.offline as pyimport mathimport os# 資料集data = pd.read_csv(‘CH3–6_顧客回購資料.csv’, encoding=’utf-8-sig’).drop(columns=[‘Unnamed: 0’])data.head()
顧客回購資料
圖1. 顧客回購資料
顧客回購資料
圖2. 擷取之顧客回購資料
chosen_data = data[data['廣告系列']=='critei']chosen_data.head(8)
回購資料集
圖3. 篩選後資料集

二、繪圖-長條圖

1. facet_col:可根據輸入欄位內的不同數值、類別,個別繪製子圖。此圖根據「第幾次」購買區分。

2. facet_col_wrap:繪製子圖的欄位數量,此圖設定為3欄。

fig = px.bar(chosen_data,x = ‘月份’, y = ‘平均利潤’, color = ‘第幾次購買’, title = ‘2018年行銷活動回購分析_critei’,facet_col=”第幾次購買”, facet_col_wrap=3)fig.show()
回購分析長條圖
圖4. 未經佈景主題美化之回購分析圖
# 修改條寬for pdata in fig.data:pdata[“width”] = 0.8# 佈景主題設定fig.update_layout(width=1600, height=800, showlegend=False, xaxis_range=(0.5, 12.5), yaxis_range=(0, 3500))fig.layout.coloraxis.showscale = False # 隱藏比例尺fig.update_xaxes(showticklabels=True, tickmode=’linear’) # 顯示x座標所有數值fig.update_yaxes(showticklabels=True) # 顯示y座標數值fig.show()
回購分析長條圖
圖5. 經佈景主題美化之回購分析圖

三、繪圖-年均利潤折線圖

print(‘2018年的平均利潤為:’ ,round(chosen_data[‘平均利潤’].mean(),2))year_mean_profit = [round(chosen_data[‘平均利潤’].mean(),2)]*12
2018年的平均利潤為: 993.16
座標參考圖
圖6. 欄列座標參考圖
print('該資料集內共有' ,math.ceil(chosen_data["第幾次購買"].nunique()), '種類型的回購資料,')print('分別為:',chosen_data["第幾次購買"].unique().tolist())total_row = math.ceil(chosen_data["第幾次購買"].nunique()/3)print('因此共有', total_row, '列的子圖')
該資料集內共有 5 種類型的回購資料,分別為: [1, 2, 3, 4, 5]因此共有 2 列的子圖
if total_row==1 :first_row_col = chosen_data["第幾次購買"].nunique()else:first_row_col = chosen_data["第幾次購買"].nunique()%3print('第一列會有', first_row_col, '張圖片')
第一列會有 2 張圖片
# 如果圖形不只有一列:if total_row !=1:# 那就透過for迴圈依序指定欄與列的編碼for row in range(1, total_row+1):# 如果現在在繪製第一列時:if row == 1:# 便根據第一列有的圖片數量給與列編碼for col in range(1, first_row_col+1):fig.add_scatter(x = list(range(1, 13)), y = year_mean_profit, name = '年每人平均利潤', row=row, col=col, line=dict(color="firebrick"), mode="lines")# 如果現在不是繪製第一列時:else:# 就依序給予1~3的列編碼for col in range(1, 4):fig.add_scatter(x = list(range(1, 13)), y = year_mean_profit, name = '年每人平均利潤', row=row, col=col, line=dict(color="firebrick"), mode="lines")# 如果圖形只有一列:else:# 便根據第一列有的圖片數量給與列編碼for col in range(1, first_row_col+1):fig.add_scatter(x = list(range(1, 13)), y = year_mean_profit, name = '年每人平均利潤', row=1, col=col, line=dict(color="firebrick"), mode="lines")fig.show()
回購分析長條圖
圖7. 加上年均線的回購分析圖

四、繪圖-月均利潤折線圖

mon_mean_profit = []for mon in chosen_data['月份'].unique():print(str(mon)+'月的平均利潤為:' ,round(chosen_data[chosen_data['月份']==mon]['平均利潤'].mean(),2))mon_mean_profit.append(round(chosen_data[chosen_data['月份']==mon]['平均利潤'].mean(),2))
1月的平均利潤為: 1112.02月的平均利潤為: 1036.03月的平均利潤為: 789.674月的平均利潤為: 732.05月的平均利潤為: 947.56月的平均利潤為: 1388.57月的平均利潤為: 740.338月的平均利潤為: 773.09月的平均利潤為: 1149.010月的平均利潤為: 854.511月的平均利潤為: 1060.512月的平均利潤為: 1178.6
# 如果圖形不只有一列:if total_row !=1:# 那就透過for迴圈依序指定欄與列的編碼for row in range(1, total_row+1):# 如果現在在繪製第一列時:if row == 1:# 便根據第一列有的圖片數量給與列編碼for col in range(1, first_row_col+1):fig.add_scatter(x = chosen_data['月份'].unique(), y = mon_mean_profit, name = '月每人平均利潤', row=row, col=col, line=dict(color="royalblue"))# 如果現在不是繪製第一列時:else:# 就依序給予1~3的列編碼for col in range(1, 4):fig.add_scatter(x = chosen_data['月份'].unique(), y = mon_mean_profit, name = '月每人平均利潤', row=row, col=col, line=dict(color="royalblue"))# 如果圖形只有一列:else:# 便根據第一列有的圖片數量給與列編碼for col in range(1, first_row_col+1):fig.add_scatter(x = chosen_data['月份'].unique(), y = mon_mean_profit, name = '月每人平均利潤', row=1, col=col, line=dict(color="royalblue"))fig.show()
月均回購分析長條圖
圖8. 加上月均線的回購分析圖

五、產出分析成果

# 迴圈逐一挑選廣告系列for ad in data['廣告系列'].unique().tolist():# 選取該廣告系列的回購資料chosen_data = data[data['廣告系列']==ad]## 繪圖-長條圖fig = px.bar(chosen_data,x = '月份', y = '平均利潤', color = '第幾次購買', title = '2018年行銷活動回購分析_'+ad,facet_col="第幾次購買", facet_col_wrap=3)# 修改條寬for pdata in fig.data:pdata["width"] = 0.8# 佈景主題設定fig.update_layout(width=1600, height=800, showlegend=False, xaxis_range=(0.5, 12.5), yaxis_range=(0, 3500))fig.layout.coloraxis.showscale = False # 隱藏比例尺fig.update_xaxes(showticklabels=True, tickmode='linear') # 顯示x座標所有數值fig.update_yaxes(showticklabels=True) # 顯示y座標數值## 繪圖-年均利潤折線圖# 計算2018年平均利潤year_mean_profit = [round(chosen_data['平均利潤'].mean(),2)]*12# 計算共有幾列子圖total_row = math.ceil(chosen_data["第幾次購買"].nunique()/3)# 計算第一列會有幾張圖片if total_row==1 :first_row_col = chosen_data["第幾次購買"].nunique()else:first_row_col = chosen_data["第幾次購買"].nunique()%3# 依序為每張子圖繪製「年均利潤折線圖」# 如果圖形不只有一列:if total_row !=1:# 那就透過for迴圈依序指定欄與列的編碼for row in range(1, total_row+1):# 如果現在在繪製第一列時:if row == 1:# 便根據第一列有的圖片數量給與列編碼for col in range(1, first_row_col+1):fig.add_scatter(x = list(range(1, 13)), y = year_mean_profit, name = '年每人平均利潤', row=row, col=col, line=dict(color="firebrick"), mode="lines")# 如果現在不是繪製第一列時:else:# 就依序給予1~3的列編碼for col in range(1, 4):fig.add_scatter(x = list(range(1, 13)), y = year_mean_profit, name = '年每人平均利潤', row=row, col=col, line=dict(color="firebrick"), mode="lines")# 如果圖形只有一列:else:# 便根據第一列有的圖片數量給與列編碼for col in range(1, first_row_col+1):fig.add_scatter(x = list(range(1, 13)), y = year_mean_profit, name = '年每人平均利潤', row=1, col=col, line=dict(color="firebrick"), mode="lines")## 繪圖-月均利潤折線圖# 計算月均利潤mon_mean_profit = []for mon in chosen_data['月份'].unique():mon_mean_profit.append(round(chosen_data[chosen_data['月份']==mon]['平均利潤'].mean(),2))# 依序為每張子圖繪製「月均利潤折線圖」# 如果圖形不只有一列:if total_row !=1:# 那就透過for迴圈依序指定欄與列的編碼for row in range(1, total_row+1):# 如果現在在繪製第一列時:if row == 1:# 便根據第一列有的圖片數量給與列編碼for col in range(1, first_row_col+1):fig.add_scatter(x = chosen_data['月份'].unique(), y = mon_mean_profit, name = '月每人平均利潤', row=row, col=col, line=dict(color="royalblue"))# 如果現在不是繪製第一列時:else:# 就依序給予1~3的列編碼for col in range(1, 4):fig.add_scatter(x = chosen_data['月份'].unique(), y = mon_mean_profit, name = '月每人平均利潤', row=row, col=col, line=dict(color="royalblue"))# 如果圖形只有一列:else:# 便根據第一列有的圖片數量給與列編碼for col in range(1, first_row_col+1):fig.add_scatter(x = chosen_data['月份'].unique(), y = mon_mean_profit, name = '月每人平均利潤', row=1, col=col, line=dict(color="royalblue"))# 將成果另存新檔py.plot(fig, filename='CH3-6產出成果_2018年行銷活動回購分析_'+ad, auto_open=True) # 網頁檔fig.write_image('CH3-6產出成果_2018年行銷活動回購分析_'+ad+'.png') # 圖檔
回購分析長條圖
圖9. 廣告系列「critei」的回購分析圖
回購分析長條圖
圖10. 廣告系列「GINEP」的回購分析圖

更多實戰案例及情境好文推薦

回到頂端