每个电子商务数据分析师必须掌握的一项数据聚类技能
如果你是一名在电子商务公司工作的数据分析师,从客户数据中挖掘潜在价值,来提高客户留存率很可能就是你的工作任务之一。
然而,客户数据是巨大的,每个客户的行为都不一样。2020年3月收购的客户A与2020年5月收购的客户B表现出不同的行为。因此,有必要将客户分为不同的群组,然后调查每个群组在一段时间内的行为。这就是所谓的同期群分析。
同期群分析是了解一个特殊客户群体在一段时间内的行为的数据分析技术。
在这篇文章中,不会详细介绍同期群分析的理论。这篇文章更多的是告诉你如何将客户分成不同的群组,并在一段时间内观察每个群组的留存率。
导入数据和python库
1 2 3 4 5 | import pandas as pd import matplotlib.pyplot as plt import seaborn as sns df = pd.read_csv( 'sales_2018-01-01_2019-12-31.csv' ) df |
分离新老客户
1 2 | first_time = df.loc[df[ 'customer_type' ] = = 'First-time' ,] final = df.loc[df[ 'customer_id' ].isin(first_time[ 'customer_id' ].values)] |
在这里,不能简单地选择df.loc[df[‘customer_type’]],因为在这个数据中,在customer_type列下,First_time指的是新客户,而Returning指的是老客户。因此,如果我在2019年12月31日第一次购买,数据会显示我在2019年12月31日是新客户,但在我第二次、第三次…时是返回客户。同期群分析着眼于新客户和他们的后续购买行为。因此,如果我们简单地使用df.loc[df[‘customer_type’]==’First-time’,],我们就会忽略新客户的后续购买,这不是分析同期群行为的正确方法。
因此,这里所需要做的是,首先创建一个所有第一次的客户列表,并将其存储为first_time。然后从原始客户数据框df中只选择那些ID在first_time客户组内的客户。通过这样做,我们可以确保我们获得的数据只有第一次的客户和他们后来的购买行为。
现在,我们删除customer_type列,因为它已经没有必要了。同时,将日期列转换成正确的日期时间格式
1 2 | final = final.drop(columns = [ 'customer_type' ]) final[ 'day' ] = pd.to_datetime(final[ 'day' ], dayfirst = True ) |
按客户ID排序,然后是日期
1 2 | final = final.drop(columns = [ 'customer_type' ]) final[ 'day' ] = pd.to_datetime(final[ 'day' ], dayfirst = True ) |
定义一些函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | def purchase_rate(customer_id): purchase_rate = [ 1 ] counter = 1 for i in range ( 1 , len (customer_id)): if customer_id[i] ! = customer_id[i - 1 ]: purchase_rate.append( 1 ) counter = 1 else : counter + = 1 purchase_rate.append(counter) return purchase_rate def join_date(date, purchase_rate): join_date = list ( range ( len (date))) for i in range ( len (purchase_rate)): if purchase_rate[i] = = 1 : join_date[i] = date[i] else : join_date[i] = join_date[i - 1 ] return join_date def age_by_month(purchase_rate, month, year, join_month, join_year): age_by_month = list ( range ( len (year))) for i in range ( len (purchase_rate)): if purchase_rate[i] = = 1 : age_by_month[i] = 0 else : if year[i] = = join_year[i]: age_by_month[i] = month[i] - join_month[i] else : age_by_month[i] = month[i] - join_month[i] + 12 * (year[i] - join_year[i]) return age_by_month |
- purchase_rate函数将决定这是否是每个客户的第二次、第三次、第四次购买。
- join_date函数允许确定客户加入的日期。
- age_by_month函数提供了从客户当前购买到第一次购买的多少个月。
现在输入已经准备好了,接下来创建群组。
创建群组
1 2 3 4 5 6 7 8 | final[ 'month' ] = pd.to_datetime(final[ 'day' ]).dt.month final[ 'Purchase Rate' ] = purchase_rate(final[ 'customer_id' ]) final[ 'Join Date' ] = join_date(final[ 'day' ], final[ 'Purchase Rate' ]) final[ 'Join Date' ] = pd.to_datetime(final[ 'Join Date' ], dayfirst = True ) final[ 'cohort' ] = pd.to_datetime(final[ 'Join Date' ]).dt.strftime( '%Y-%m' ) final[ 'year' ] = pd.to_datetime(final[ 'day' ]).dt.year final[ 'Join Date Month' ] = pd.to_datetime(final[ 'Join Date' ]).dt.month final[ 'Join Date Year' ] = pd.to_datetime(final[ 'Join Date' ]).dt.year |
1 2 3 4 5 | final[ 'Age by month' ] = age_by_month(final[ 'Purchase Rate' ], final[ 'month' ], final[ 'year' ], final[ 'Join Date Month' ], final[ 'Join Date Year' ]) |
1 2 3 4 | cohorts = final.groupby([ 'cohort' , 'Age by month' ]).nunique() cohorts = cohorts.customer_id.to_frame().reset_index() # convert series to frame cohorts = pd.pivot_table(cohorts, values = 'customer_id' ,index = 'cohort' , columns = 'Age by month' ) cohorts.replace(np.nan, '',regex = True ) |
**如何解释这个表格:**以群组2018-01为例。在2018年1月,有462名新客户。在这462人中,121名客户在2018年2月回来购买,125名在2018年3月购买,以此类推。
转换为群组百分比
1 2 3 | for i in range ( len (cohorts) - 1 ): cohorts[i + 1 ] = cohorts[i + 1 ] / cohorts[ 0 ] cohorts[ 0 ] = cohorts[ 0 ] / cohorts[ 0 ] |
可视化
1 2 3 4 5 6 7 8 9 10 | cohorts_t = cohorts.transpose() cohorts_t[cohorts_t.columns].plot(figsize = ( 10 , 5 )) sns. set (style = 'whitegrid' ) plt.figure(figsize = ( 20 , 15 )) plt.title( 'Cohorts: User Retention' ) sns. set (font_scale = 0.5 ) # font size sns.heatmap(cohorts, mask = cohorts.isnull(), cmap = "Blues" , annot = True , fmt = '.01%' ) plt.show() |
到此这篇关于详解如何利用Python进行客户分群分析的文章就介绍到这了,更多相关Python客户分群分析内容请搜索IT俱乐部以前的文章或继续浏览下面的相关文章希望大家以后多多支持IT俱乐部!