4.3.1生成购买最多的类目(df_cate_Order)
1.数据概况
数据概况:
customer_id :用户ID
product_id :产品名
action_date :动作执行时间(2018/2/1-2018-4/15)
action_id :动作ID
type :'PageView':浏览, 'Follow':收藏, 'Order':购买, 'SavedCart'加入购物车, 'Comment':评论
age_range :年龄等级1-6
gender :男M女W,后续替换成1和0,方便计算
customer_register_date :用户注册时间
customer_level :用户等级1-7
city_level :用户所在城市等级1-6,大概是超一线城市到五线城市
brand :品牌
shop_id : 店铺名称
category :商品分类
product_market_date :商品上架日期
vender_id :仓库代码
fans_number :收藏人数
vip_number :这个真没看出来,得去找上传数据的人问清楚
shop_register_date :店铺注册时间,但是根据抽样结果来看,有38.56%的缺失,对结果的影响比较大
shop_category :店铺分类
shop_score:店铺评分
2.观察数据的具体情况
#数据清洗
import numpy as np
import pandas as pd
import gc
import warnings
warnings.filterwarnings('ignore')
from datetime import datetime
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['SimHei']
# 用来设置字体样式以正常显示中文标签
plt.rcParams['axes.unicode_minus']=False
# 默认是使用Unicode负号,设置正常显示字符,如正常显示负号
#读取数据
df_original = pd.read_excel('京东消费者分析数据.xlsx')
df=df_original.sample(frac=0.3,random_state =None)
del df_original
gc.collect()
print(df)
df.info()
df.isnull().sum()
df.describe()
Out:
<class 'pandas.core.frame.DataFrame'> Int64Index: 55148 entries, 107652 to 183471 Data columns (total 20 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 customer_id 55148 non-null int64 1 product_id 55148 non-null int64 2 action_date 55148 non-null datetime64[ns] 3 action_id 55148 non-null int64 4 type 55148 non-null object 5 age_range 55118 non-null float64 6 gender 55148 non-null object 7 customer_register_date 55148 non-null datetime64[ns] 8 customer_level 55148 non-null int64 9 city_level 55107 non-null float64 10 brand 55148 non-null object 11 shop_id 55148 non-null int64 12 category 55148 non-null object 13 product_market_date 55148 non-null datetime64[ns] 14 vender_id 55148 non-null int64 15 fans_number 55148 non-null int64 16 vip_number 55148 non-null int64 17 shop_register_date 33574 non-null datetime64[ns] 18 shop_category 55148 non-null object 19 shop_score 55148 non-null float64 dtypes: datetime64[ns](4), float64(3), int64(8), object(5) memory usage: 8.8+ MB
Out:
customer_id 0 product_id 0 action_date 0 action_id 0 type 0 age_range 30 gender 0 customer_register_date 0 customer_level 0 city_level 41 brand 0 shop_id 0 category 0 product_market_date 0 vender_id 0 fans_number 0 vip_number 0 shop_register_date 21574 shop_category 0 shop_score 0 dtype: int64
customer_id | product_id | action_id | age_range | customer_level | city_level | shop_id | vender_id | fans_number | vip_number | shop_score | |
---|---|---|---|---|---|---|---|---|---|---|---|
count | 5.514800e+04 | 55148.000000 | 5.514800e+04 | 55118.000000 | 55148.000000 | 55107.000000 | 55148.000000 | 55148.000000 | 5.514800e+04 | 5.514800e+04 | 55148.000000 |
mean | 8.023935e+05 | 188737.670541 | 5.919336e+06 | 4.676385 | 4.967379 | 3.282632 | 5275.254968 | 4319.971894 | 6.875368e+04 | 1.019048e+05 | 5.685935 |
std | 4.642326e+05 | 108832.344680 | 3.355640e+06 | 1.531193 | 2.334062 | 1.415285 | 2909.456996 | 2216.399499 | 2.467923e+05 | 2.707927e+05 | 4.658851 |
min | 2.900000e+01 | 11.000000 | 9.700000e+01 | 1.000000 | 1.000000 | 1.000000 | 2.000000 | 1.000000 | 0.000000e+00 | 0.000000e+00 | -1.000000 |
25% | 3.994782e+05 | 94598.000000 | 3.015719e+06 | 5.000000 | 5.000000 | 3.000000 | 2836.000000 | 3666.000000 | 0.000000e+00 | 0.000000e+00 | 0.000000 |
50% | 8.006390e+05 | 187265.000000 | 5.979096e+06 | 5.000000 | 6.000000 | 4.000000 | 5523.000000 | 3666.000000 | 4.440000e+03 | 1.247200e+04 | 9.305797 |
75% | 1.205906e+06 | 283693.500000 | 8.958055e+06 | 6.000000 | 7.000000 | 4.000000 | 7794.000000 | 5527.250000 | 4.262900e+04 | 9.209900e+04 | 9.537014 |
max | 1.608703e+06 | 378410.000000 | 1.136353e+07 | 6.000000 | 7.000000 | 6.000000 | 10394.000000 | 9751.000000 | 9.293487e+06 | 1.384168e+07 | 10.000000 |
df['type'].unique()
#去除type中的重复元素
Out:array(['PageView', 'Order', 'Comment', 'Follow', 'SavedCart'], dtype=object)
df['action_id'].value_counts()
10439703 2689 4099416 4 6069655 4 9401782 4 1553885 3 ... 199997 1 8050723 1 1858919 1 2400243 1 7382395 1 Name: action_id, Length: 51971, dtype: int64
df[df['action_id']==10439703]
customer_id | product_id | action_date | action_id | type | age_range | gender | customer_register_date | customer_level | city_level | brand | shop_id | category | product_market_date | vender_id | fans_number | vip_number | shop_register_date | shop_category | shop_score | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
96135 | 565504 | 70180 | 2018-03-02 19:01:00 | 10439703 | Comment | 6.0 | W | 2017-10-30 | 1 | 1.0 | Haier | 9439 | Refrigerator | 2017-07-19 | 6518 | 13481 | 82569 | 2017-07-13 | Household Eletric Appliance | 9.732879 |
92972 | 1043570 | 338841 | 2018-04-02 13:41:00 | 10439703 | Comment | 4.0 | W | 2017-01-17 | 1 | 1.0 | Other | 8156 | Cookie | 2017-06-29 | 4483 | 25898 | 118681 | 2017-06-06 | Food | 9.546145 |
91826 | 665326 | 327826 | 2018-03-27 13:46:00 | 10439703 | Comment | 5.0 | W | 2011-04-29 | 7 | 3.0 | Three Squirrels | 8111 | Nut | 2014-11-10 | 6502 | 124511 | 159554 | 2013-06-28 | Food | 9.382478 |
94609 | 1607604 | 363796 | 2018-02-23 23:33:00 | 10439703 | Follow | 4.0 | M | 2014-07-25 | 5 | 5.0 | Other | 4114 | Coat | 2016-03-03 | 5321 | 1352 | 8786 | 2013-08-26 | Clothes | 9.291452 |
96124 | 1375615 | 19643 | 2018-03-18 14:27:00 | 10439703 | Comment | 5.0 | W | 2018-03-07 | 6 | 1.0 | Loreal | 4349 | Face Cream | 2017-09-19 | 6551 | 27837 | 279449 | 2017-07-27 | Beauty Makeup | 9.615594 |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
90942 | 179716 | 271781 | 2018-04-14 13:07:00 | 10439703 | SavedCart | 6.0 | W | 2010-06-26 | 7 | 3.0 | Gree | 2197 | Air Conditioner | 2018-02-28 | 3306 | 6727 | 39587 | 2017-11-20 | Household Eletric Appliance | 9.907947 |
97824 | 1605985 | 259654 | 2018-04-12 07:05:00 | 10439703 | SavedCart | 5.0 | M | 2015-06-13 | 6 | 1.0 | Other | 1756 | Facial Mask | 2015-09-12 | 1271 | 4217 | 20153 | 2015-09-11 | Beauty Makeup | 9.389397 |
93014 | 40098 | 348817 | 2018-04-13 19:25:00 | 10439703 | SavedCart | 2.0 | M | 2013-05-18 | 7 | 4.0 | Lenovo | 6662 | Notebook | 2015-03-10 | 3666 | 0 | 0 | NaT | Electronics | 0.000000 |
91310 | 1547218 | 277565 | 2018-04-11 15:03:00 | 10439703 | SavedCart | 5.0 | W | 2017-03-04 | 1 | 3.0 | Kuka | 4943 | Sofa | 2017-03-27 | 3658 | 2965 | 17371 | 2017-03-26 | Furniture | 9.692748 |
96453 | 1356675 | 242824 | 2018-04-10 23:37:00 | 10439703 | SavedCart | 1.0 | W | 2016-03-01 | 6 | 3.0 | Other | 9031 | Makerup Water | 2015-03-26 | 5127 | 8972 | 71441 | 2015-03-20 | Beauty Makeup | 9.031195 |
2689 rows × 20 columns
理论上说,一个action_id应该是针对一个用户一次动作在系统中的标志,应该是具有唯一性的,但是这个action_id在不同用户的不同场景下重复使用,因为这个数据集只是随机抽样了30%的数据,可以理解为系统随机生成且重复使用的。
#查看产品和商品,大概能知道哪一家店和商品的热度较高
df['product_id'].value_counts()
df['shop_id'].value_counts()
#查看仓库数据,最多的应该是京东自己的仓库
df['vender_id'].value_counts()
#查看注册时间,发现根据抽样结果来看,有38.56%的缺失,对结果的影响比较大
df['shop_register_date'].value_counts()
#删除shop_register_date
df.drop('shop_register_date',axis=1,inplace=True)
3.对有关数据进行替换
3.1把性别替换成可识别的1和0,把行为替换为可识别的1-2-3-4-5
#把性别替换成可识别的1和0
df.loc[df['gender'] == 'M', 'gender'] = 1
df.loc[df['gender'] == 'W', 'gender'] = 0
#把行为替换为可识别的1-2-3-4-5
df.loc[df['type'] == 'PageView', 'type'] = 1
df.loc[df['type'] == 'Follow', 'type'] = 2
df.loc[df['type'] == 'SavedCart', 'type'] = 3
df.loc[df['type'] == 'Order', 'type'] = 4
df.loc[df['type'] == 'Comment', 'type'] = 5
df
3.2 用户最喜欢浏览和下单的时间
df['date'] = df['action_date'].dt.date
df['time'] = df['action_date'].dt.hour
df['Zone'] = pd.cut(df['time'],bins=[-1,5,10,13,18,24],labels=['凌晨','上午','中午','下午','晚上'])
#把时间段分为'凌晨'、'上午'、'中午'、'下午'、'晚上'
df.drop('action_date',axis=1,inplace=True)
df
3.2.1 用户(labels)
users = df['customer_id'].unique()
labels = pd.DataFrame(users,columns=['customer_id'])
labels
3.2.2 用户活跃时间(time_browse)
time_browse=df[df['type']==1].groupby(['customer_id','Zone']).product_id.count().reset_index()
time_browse.rename(columns={'product_id':'hour_counts'}, inplace=True)
time_browse
3.2.3 每个用户浏览次数最多的时间(time_browse_max)
#每个用户浏览次数最多的时段
time_browse_max = time_browse.groupby('customer_id').hour_counts.max().reset_index()
time_browse_max.rename(columns={'hour_counts':'read_counts_max'},inplace=True)
time_browse_max
#重新构建time_browse
time_browse = pd.merge(time_browse,time_browse_max,how='left',on='customer_id')
time_browse
3.2.3 每个用户浏览次数最多的时段(time_browse_Zone)
#选取各用户浏览次数最多的时段,如有并列最多的时段,用逗号连接
time_browse_Zone=time_browse.loc[time_browse['hour_counts']==time_browse['read_counts_max'],'Zone'].groupby(time_browse['customer_id']).aggregate(lambda x:','.join(x)).reset_index()
time_browse_Zone
3.2.4将用户浏览的时段加入到用户标签表(lables)中
labels = pd.merge(labels,time_browse_Zone,how='left',on='customer_id')
labels.rename(columns={'Zone':'time_browse'},inplace=True)
#labels#因为抽样数据,所以出现空值也很正常
labels
3.2.5得出客户最喜欢下单的时间段(time_Order_hour)
#得出客户最喜欢下单的时间段
time_Order = df[df['type']==4].groupby(['customer_id','Zone']).product_id.count().reset_index()
time_Order.rename(columns={'product_id':'hour_counts'},inplace=True)
time_Order_max = time_Order.groupby('customer_id').hour_counts.max().reset_index()
time_Order_max.rename(columns={'hour_counts':'buy_counts_max'},inplace=True)
time_Order = pd.merge(time_Order,time_Order_max,how='left',on='customer_id')
time_Order_hour = time_Order.loc[time_Order['hour_counts']==time_Order['buy_counts_max'],'Zone'].groupby(time_Order['customer_id']).aggregate(lambda x:','.join(x)).reset_index()
time_Order_hour
3.2.6客户最喜欢下单的时间段用户标签表(lables)中
labels = pd.merge(labels,time_Order_hour,how='left',on='customer_id')
labels.rename(columns={'Zone':'time_Order'},inplace=True)
labels
4.商品类别作为锚点,进行店铺以及品牌的推荐
df_browse = df.loc[df['type']==1,['customer_id','product_id','brand','category','shop_category']]#浏览
df_Follow = df.loc[df['type']==2,['customer_id','product_id','brand','category','shop_category']]#关注
df_SavedCart = df.loc[df['type']==3,['customer_id','product_id','brand','category','shop_category']]#加入购物车
df_Order = df.loc[df['type']==4,['customer_id','product_id','brand','category','shop_category']]#购买
df_Comment = df.loc[df['type']==5,['customer_id','product_id','brand','category','shop_category']]#评论
4.1浏览最多的类目
#对用户与类目进行分组,统计浏览次数
df_cate_most_browse = df_browse.groupby(['customer_id','category']).product_id.count().reset_index()
df_cate_most_browse.rename(columns={'product_id':'category_counts'},inplace=True)
#每个用户浏览次数最多的类目
df_cate_most_browse_max = df_cate_most_browse.groupby('customer_id').category_counts.max().reset_index()
df_cate_most_browse_max.rename(columns={'category_counts':'category_counts_max'},inplace=True)
4.1.1各用户浏览次数最多的类目(df_cate_browse)
df_cate_most_browse = pd.merge(df_cate_most_browse,df_cate_most_browse_max,how='left',on='customer_id')
df_cate_most_browse['category'] = df_cate_most_browse['category'].astype(str)
#选取各用户浏览次数最多的类目,如有并列最多的类目,用逗号连接
df_cate_browse = df_cate_most_browse.loc[df_cate_most_browse['category_counts']==df_cate_most_browse['category_counts_max'],'category'].groupby(df_cate_most_browse['customer_id']).aggregate(lambda x:','.join(x)).reset_index()
df_cate_browse
4.1.2将用户浏览最多的类目加入到用户标签表(lables)中
#将用户浏览最多的类目加入到用户标签表中
labels = pd.merge(labels,df_cate_browse,how='left',on='customer_id')
labels.rename(columns={'category':'cate_most_browse'},inplace=True)
labels
4.2关注最多的目录
4.2.1生成产品与浏览最多的类目(df_cate_Follow)
#生成产品与浏览最多的类目
df_cate_most_Follow = df_Follow.groupby(['customer_id','category']).product_id.count().reset_index()
df_cate_most_Follow.rename(columns={'product_id':'category_counts'},inplace=True)
df_cate_most_Follow_max = df_cate_most_Follow.groupby('customer_id').category_counts.max().reset_index()
df_cate_most_Follow_max.rename(columns={'category_counts':'category_counts_max'},inplace=True)
df_cate_most_Follow = pd.merge(df_cate_most_Follow,df_cate_most_Follow_max,how='left',on='customer_id')
df_cate_most_Follow['category'] = df_cate_most_Follow['category'].astype(str)
df_cate_Follow = df_cate_most_Follow.loc[df_cate_most_Follow['category_counts']==df_cate_most_Follow['category_counts_max'],'category'].groupby(df_cate_most_Follow['customer_id']).aggregate(lambda x:','.join(x)).reset_index()
df_cate_Follow
4.2.2生成产品与浏览最多的类目加入到用户标签表(lables)中
labels = pd.merge(labels,df_cate_Follow,how='left',on='customer_id')
labels.rename(columns={'category':'cate_most_Follow'},inplace=True)
labels
4.2.3用最大加购数定位收藏种类,以‘,’分割
df_cate_most_SavedCart = df_SavedCart.groupby(['customer_id','category']).product_id.count().reset_index()#根据用户id和加购类目统计每个类目下有多少个ITEM
df_cate_most_SavedCart.rename(columns={'product_id':'category_counts'},inplace=True)#更改item计数列名称
df_cate_most_SavedCart_max = df_cate_most_SavedCart.groupby('customer_id').category_counts.max().reset_index()#用户加购item最多的数量
df_cate_most_SavedCart_max.rename(columns={'category_counts':'category_counts_max'},inplace=True)#更改加购计数列名
df_cate_most_SavedCart = pd.merge(df_cate_most_SavedCart,df_cate_most_SavedCart_max,how='left',on='customer_id')
df_cate_most_SavedCart['category'] = df_cate_most_SavedCart['category'].astype(str)
df_cate_SavedCart = df_cate_most_SavedCart.loc[df_cate_most_SavedCart['category_counts']==df_cate_most_SavedCart['category_counts_max'],'category'].groupby(df_cate_most_SavedCart['customer_id']).aggregate(lambda x:','.join(x)).reset_index()
# 用最大加购数定位收藏种类,以','分割
df_cate_most_SavedCart
4.2.4将最大加购数定位收藏种类加入到用户标签表(lables)中
labels = pd.merge(labels,df_cate_SavedCart,how='left',on='customer_id')
labels.rename(columns={'category':'cate_most_SavedCart'},inplace=True)
labels
customer_id | time_browse | time_Order | cate_most_browse | cate_most_Follow | cate_most_SavedCart | |
---|---|---|---|---|---|---|
0 | 1458305 | 下午 | NaN | Tablet | NaN | NaN |
1 | 1467825 | 晚上 | NaN | Coat | NaN | NaN |
2 | 275569 | 下午 | NaN | Badminton | NaN | NaN |
3 | 1015490 | 晚上 | NaN | Foundation Make-up | NaN | NaN |
4 | 1467471 | 凌晨 | NaN | Notebook | NaN | NaN |
... | ... | ... | ... | ... | ... | ... |
48848 | 588171 | 凌晨 | NaN | Cookie | NaN | NaN |
48849 | 157689 | 晚上 | NaN | Sunscreen Cream | NaN | NaN |
48850 | 809793 | 下午 | NaN | Phone | NaN | NaN |
48851 | 441831 | 中午 | NaN | Phone | NaN | NaN |
48852 | 1402044 | NaN | NaN | NaN | NaN | NaN |
48853 rows × 6 columns
4.3购买最多的类目
4.3.1生成购买最多的类目(df_cate_Order)
df_cate_most_Order = df_Order.groupby(['customer_id','category']).product_id.count().reset_index()
df_cate_most_Order.rename(columns={'product_id':'category_counts'},inplace=True)
df_cate_most_Order_max = df_cate_most_Order.groupby('customer_id').category_counts.max().reset_index()
df_cate_most_Order_max.rename(columns={'category_counts':'category_counts_max'},inplace=True)
df_cate_most_Order = pd.merge(df_cate_most_Order,df_cate_most_Order_max,how='left',on='customer_id')
df_cate_most_Order['category'] = df_cate_most_Order['category'].astype(str)
df_cate_Order = df_cate_most_Order.loc[df_cate_most_Order['category_counts']==df_cate_most_Order['category_counts_max'],'category'].groupby(df_cate_most_Order['customer_id']).aggregate(lambda x:','.join(x)).reset_index()
df_cate_Order
4.3.2将生成购买最多的类目加入到用户标签表(lables)中
labels = pd.merge(labels,df_cate_Order,how='left',on='customer_id')
labels.rename(columns={'category':'cate_most_Order'},inplace=True)
labels
4.4 30天内用户行为(month_active)
数据集的最后一个action_date是2018/04/15,往前倒30天就是2018/03/16
df_short = df[['customer_id','product_id','type','brand','category','shop_category','date']]
df_short['date'] = pd.to_datetime(df_short['date'],format='%Y-%m-%d')
df_short
df_near_30 = df_short[df_short['date']>datetime.strptime('2018-03-15', '%Y-%m-%d')]
#df_near_30
df_near_30_Order = df_near_30[df_near_30['type']==4].groupby('customer_id').product_id.count().reset_index()
labels = pd.merge(labels,df_near_30_Order,how='left',on='customer_id')
labels.rename(columns={'product_id':'month_buy'},inplace=True)
df_near_30_SavedCart = df_near_30[df_near_30['type']==3].groupby('customer_id').product_id.count().reset_index()
labels = pd.merge(labels,df_near_30_SavedCart,how='left',on='customer_id')
labels.rename(columns={'product_id':'month_Cart'},inplace=True)
month_active = df_near_30.groupby('customer_id')['date'].nunique()
labels = pd.merge(labels,month_active,how='left',on='customer_id')
labels.rename(columns={'date':'month_active'},inplace=True)
labels
customer_id | time_browse | time_Order | cate_most_browse | cate_most_Follow | cate_most_SavedCart | cate_most_Order | month_buy | month_Cart | month_active | |
---|---|---|---|---|---|---|---|---|---|---|
0 | 1458305 | 下午 | NaN | Tablet | NaN | NaN | NaN | NaN | NaN | NaN |
1 | 1467825 | 晚上 | NaN | Coat | NaN | NaN | NaN | NaN | NaN | NaN |
2 | 275569 | 下午 | NaN | Badminton | NaN | NaN | NaN | NaN | NaN | 1.0 |
3 | 1015490 | 晚上 | NaN | Foundation Make-up | NaN | NaN | NaN | NaN | NaN | 1.0 |
4 | 1467471 | 凌晨 | NaN | Notebook | NaN | NaN | NaN | NaN | NaN | 1.0 |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
48848 | 588171 | 凌晨 | NaN | Cookie | NaN | NaN | NaN | NaN | NaN | 1.0 |
48849 | 157689 | 晚上 | NaN | Sunscreen Cream | NaN | NaN | NaN | NaN | NaN | NaN |
48850 | 809793 | 下午 | NaN | Phone | NaN | NaN | NaN | NaN | NaN | NaN |
48851 | 441831 | 中午 | NaN | Phone | NaN | NaN | NaN | NaN | NaN | NaN |
48852 | 1402044 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | 1.0 |
48853 rows × 10 columns
4.5 7天内用户行为(df_week_buy)
数据集的最后一个action_date是2018/04/15,往前倒7天就是2018/04/07
df_week = df_short[df_short['date']>datetime.strptime('2018-04-07', '%Y-%m-%d')]
df_week_buy = df_week[df_week['type']==4].groupby('customer_id').product_id.count().reset_index()
labels = pd.merge(labels,df_week_buy,how='left',on='customer_id')
labels.rename(columns={'product_id':'week_buy'},inplace=True)
df_week_SavedCart = df_week[df_week['type']==3].groupby('customer_id').product_id.count().reset_index()
labels = pd.merge(labels,df_week_SavedCart,how='left',on='customer_id')
labels.rename(columns={'product_id':'week_Cart'},inplace=True)
week_active = df_near_30.groupby('customer_id')['date'].nunique()
labels = pd.merge(labels,week_active,how='left',on='customer_id')
labels.rename(columns={'date':'week_active'},inplace=True)
labels
customer_id | time_browse | time_Order | cate_most_browse | cate_most_Follow | cate_most_SavedCart | cate_most_Order | month_buy | month_Cart | month_active | week_buy | week_Cart | week_active | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 1458305 | 下午 | NaN | Tablet | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
1 | 1467825 | 晚上 | NaN | Coat | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
2 | 275569 | 下午 | NaN | Badminton | NaN | NaN | NaN | NaN | NaN | 1.0 | NaN | NaN | 1.0 |
3 | 1015490 | 晚上 | NaN | Foundation Make-up | NaN | NaN | NaN | NaN | NaN | 1.0 | NaN | NaN | 1.0 |
4 | 1467471 | 凌晨 | NaN | Notebook | NaN | NaN | NaN | NaN | NaN | 1.0 | NaN | NaN | 1.0 |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
48848 | 588171 | 凌晨 | NaN | Cookie | NaN | NaN | NaN | NaN | NaN | 1.0 | NaN | NaN | 1.0 |
48849 | 157689 | 晚上 | NaN | Sunscreen Cream | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
48850 | 809793 | 下午 | NaN | Phone | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
48851 | 441831 | 中午 | NaN | Phone | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
48852 | 1402044 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | 1.0 | NaN | NaN | 1.0 |
48853 rows × 13 columns
4.6上次浏览(last_browse)、加购(last_SavedCart
)、购买(last_Order)距最后一天(2018/04/15)的天数
#上次浏览距今(2018/04/15)天数
last_browse = df_short[df_short['type']==1].groupby('customer_id')['date'].max().apply(lambda x:(datetime.strptime('2018-04-15','%Y-%m-%d')-x).days)
labels = pd.merge(labels,last_browse,how='left',on='customer_id')
labels.rename(columns={'date':'last_browse'},inplace=True)
labels
#上次加购距今(2018/04/15)天数
last_SavedCart = df_short[df_short['type']==3].groupby('customer_id')['date'].max().apply(lambda x:(datetime.strptime('2018-04-15','%Y-%m-%d')-x).days)
labels = pd.merge(labels,last_SavedCart,how='left',on='customer_id')
labels.rename(columns={'date':'last_SavedCart'},inplace=True)
labels
#上次购买距今(2018/04/15)天数
last_Order = df_short[df_short['type']==4].groupby('customer_id')['date'].max().apply(lambda x:(datetime.strptime('2018-04-15','%Y-%m-%d')-x).days)
labels = pd.merge(labels,last_Order,how='left',on='customer_id')
labels.rename(columns={'date':'last_Orger'},inplace=True)
labels
customer_id | time_browse | time_Order | cate_most_browse | cate_most_Follow | cate_most_SavedCart | cate_most_Order | month_buy | month_Cart | month_active | week_buy | week_Cart | week_active | last_browse | last_SavedCart | last_Orger | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 1458305 | 下午 | NaN | Tablet | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | 51.0 | NaN | NaN |
1 | 1467825 | 晚上 | NaN | Coat | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | 69.0 | NaN | NaN |
2 | 275569 | 下午 | NaN | Badminton | NaN | NaN | NaN | NaN | NaN | 1.0 | NaN | NaN | 1.0 | 1.0 | NaN | NaN |
3 | 1015490 | 晚上 | NaN | Foundation Make-up | NaN | NaN | NaN | NaN | NaN | 1.0 | NaN | NaN | 1.0 | 25.0 | NaN | NaN |
4 | 1467471 | 凌晨 | NaN | Notebook | NaN | NaN | NaN | NaN | NaN | 1.0 | NaN | NaN | 1.0 | 5.0 | NaN | NaN |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
48848 | 588171 | 凌晨 | NaN | Cookie | NaN | NaN | NaN | NaN | NaN | 1.0 | NaN | NaN | 1.0 | 20.0 | NaN | NaN |
48849 | 157689 | 晚上 | NaN | Sunscreen Cream | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | 46.0 | NaN | NaN |
48850 | 809793 | 下午 | NaN | Phone | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | 34.0 | NaN | NaN |
48851 | 441831 | 中午 | NaN | Phone | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | 59.0 | NaN | NaN |
48852 | 1402044 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | 1.0 | NaN | NaN | 1.0 | NaN | NaN | NaN |
48853 rows × 16 columns
4.7最近两次购买间隔天数(interval_buy)
#最近两次购买间隔天数
df_interval_buy = df_short[df_short['type']==4].groupby(['customer_id','date']).product_id.count().reset_index()
interval_buy = df_interval_buy.groupby('customer_id')['date'].apply(lambda x:x.sort_values().diff(1).dropna().head(1)).reset_index()
interval_buy['date'] = interval_buy['date'].apply(lambda x : x.days)
interval_buy.drop('level_1',axis=1,inplace=True)
interval_buy.rename(columns={'date':'interval_buy'},inplace=True)
labels = pd.merge(labels,interval_buy,how='left',on='customer_id')
labels
customer_id | time_browse | time_Order | cate_most_browse | cate_most_Follow | cate_most_SavedCart | cate_most_Order | month_buy | month_Cart | month_active | week_buy | week_Cart | week_active | last_browse | last_SavedCart | last_Orger | interval_buy | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 1458305 | 下午 | NaN | Tablet | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | 51.0 | NaN | NaN | NaN |
1 | 1467825 | 晚上 | NaN | Coat | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | 69.0 | NaN | NaN | NaN |
2 | 275569 | 下午 | NaN | Badminton | NaN | NaN | NaN | NaN | NaN | 1.0 | NaN | NaN | 1.0 | 1.0 | NaN | NaN | NaN |
3 | 1015490 | 晚上 | NaN | Foundation Make-up | NaN | NaN | NaN | NaN | NaN | 1.0 | NaN | NaN | 1.0 | 25.0 | NaN | NaN | NaN |
4 | 1467471 | 凌晨 | NaN | Notebook | NaN | NaN | NaN | NaN | NaN | 1.0 | NaN | NaN | 1.0 | 5.0 | NaN | NaN | NaN |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
48848 | 588171 | 凌晨 | NaN | Cookie | NaN | NaN | NaN | NaN | NaN | 1.0 | NaN | NaN | 1.0 | 20.0 | NaN | NaN | NaN |
48849 | 157689 | 晚上 | NaN | Sunscreen Cream | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | 46.0 | NaN | NaN | NaN |
48850 | 809793 | 下午 | NaN | Phone | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | 34.0 | NaN | NaN | NaN |
48851 | 441831 | 中午 | NaN | Phone | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | 59.0 | NaN | NaN | NaN |
48852 | 1402044 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | 1.0 | NaN | NaN | 1.0 | NaN | NaN | NaN | NaN |
48853 rows × 17 columns
4.8用户浏览但是并未购买(browse_not_buy)
df_browse_buy = df.loc[(df['type']==1) | (df['type']==4),['customer_id','product_id','type','time']]
browse_not_buy = pd.pivot_table(df_browse_buy,index=['customer_id','product_id'],columns=['type'],values=['time'],aggfunc=['count'])#
browse_not_buy.columns = ['browse','buy']#分列
browse_not_buy.fillna(0,inplace=True)#把空值替换成0
browse_not_buy['browse_not_buy'] = 0
browse_not_buy.loc[(browse_not_buy['browse']>0) & (browse_not_buy['buy']==0),'browse_not_buy'] = 1#标志浏览缺没下单的为1
browse_not_buy = browse_not_buy.groupby('customer_id')['browse_not_buy'].sum().reset_index()
labels = pd.merge(labels,browse_not_buy,how='left',on='customer_id')
labels['browse_not_buy'] = labels['browse_not_buy'].apply(lambda x: '是' if x>0 else '否')
labels
customer_id | time_browse | time_Order | cate_most_browse | cate_most_Follow | cate_most_SavedCart | cate_most_Order | month_buy | month_Cart | month_active | week_buy | week_Cart | week_active | last_browse | last_SavedCart | last_Orger | interval_buy | browse_not_buy | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 1458305 | 下午 | NaN | Tablet | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | 51.0 | NaN | NaN | NaN | 是 |
1 | 1467825 | 晚上 | NaN | Coat | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | 69.0 | NaN | NaN | NaN | 是 |
2 | 275569 | 下午 | NaN | Badminton | NaN | NaN | NaN | NaN | NaN | 1.0 | NaN | NaN | 1.0 | 1.0 | NaN | NaN | NaN | 是 |
3 | 1015490 | 晚上 | NaN | Foundation Make-up | NaN | NaN | NaN | NaN | NaN | 1.0 | NaN | NaN | 1.0 | 25.0 | NaN | NaN | NaN | 是 |
4 | 1467471 | 凌晨 | NaN | Notebook | NaN | NaN | NaN | NaN | NaN | 1.0 | NaN | NaN | 1.0 | 5.0 | NaN | NaN | NaN | 是 |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
48848 | 588171 | 凌晨 | NaN | Cookie | NaN | NaN | NaN | NaN | NaN | 1.0 | NaN | NaN | 1.0 | 20.0 | NaN | NaN | NaN | 是 |
48849 | 157689 | 晚上 | NaN | Sunscreen Cream | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | 46.0 | NaN | NaN | NaN | 是 |
48850 | 809793 | 下午 | NaN | Phone | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | 34.0 | NaN | NaN | NaN | 是 |
48851 | 441831 | 中午 | NaN | Phone | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | 59.0 | NaN | NaN | NaN | 是 |
48852 | 1402044 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | 1.0 | NaN | NaN | 1.0 | NaN | NaN | NaN | NaN | 否 |
48853 rows × 18 columns
4.9用户加购但是并未购买(cart_not_buy)
df_cart_buy = df.loc[(df['type']==1) | (df['type']==3),['customer_id','product_id','type','time']]
cart_not_buy = pd.pivot_table(df_cart_buy,index=['customer_id','product_id'],columns=['type'],values=['time'],aggfunc=['count'])
cart_not_buy.columns = ['cart','buy']
cart_not_buy.fillna(0,inplace=True)
cart_not_buy['cart_not_buy'] = 0
cart_not_buy.loc[(cart_not_buy['cart']>0) & (cart_not_buy['buy']==0),'cart_not_buy'] = 1
cart_not_buy = cart_not_buy.groupby('customer_id')['cart_not_buy'].sum().reset_index()
labels = pd.merge(labels,cart_not_buy,how='left',on='customer_id')
labels['cart_not_buy'] = labels['cart_not_buy'].apply(lambda x: '是' if x>0 else '否')
labels
customer_id | time_browse | time_Order | cate_most_browse | cate_most_Follow | cate_most_SavedCart | cate_most_Order | month_buy | month_Cart | month_active | ... | week_Cart | week_active | last_browse | last_SavedCart | last_Orger | interval_buy | browse_not_buy | cart_not_buy_x | cart_not_buy_y | cart_not_buy | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 1458305 | 下午 | NaN | Tablet | NaN | NaN | NaN | NaN | NaN | NaN | ... | NaN | NaN | 51.0 | NaN | NaN | NaN | 是 | 是 | 1.0 | 是 |
1 | 1467825 | 晚上 | NaN | Coat | NaN | NaN | NaN | NaN | NaN | NaN | ... | NaN | NaN | 69.0 | NaN | NaN | NaN | 是 | 是 | 1.0 | 是 |
2 | 275569 | 下午 | NaN | Badminton | NaN | NaN | NaN | NaN | NaN | 1.0 | ... | NaN | 1.0 | 1.0 | NaN | NaN | NaN | 是 | 是 | 1.0 | 是 |
3 | 1015490 | 晚上 | NaN | Foundation Make-up | NaN | NaN | NaN | NaN | NaN | 1.0 | ... | NaN | 1.0 | 25.0 | NaN | NaN | NaN | 是 | 是 | 1.0 | 是 |
4 | 1467471 | 凌晨 | NaN | Notebook | NaN | NaN | NaN | NaN | NaN | 1.0 | ... | NaN | 1.0 | 5.0 | NaN | NaN | NaN | 是 | 是 | 1.0 | 是 |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
48848 | 588171 | 凌晨 | NaN | Cookie | NaN | NaN | NaN | NaN | NaN | 1.0 | ... | NaN | 1.0 | 20.0 | NaN | NaN | NaN | 是 | 是 | 1.0 | 是 |
48849 | 157689 | 晚上 | NaN | Sunscreen Cream | NaN | NaN | NaN | NaN | NaN | NaN | ... | NaN | NaN | 46.0 | NaN | NaN | NaN | 是 | 是 | 1.0 | 是 |
48850 | 809793 | 下午 | NaN | Phone | NaN | NaN | NaN | NaN | NaN | NaN | ... | NaN | NaN | 34.0 | NaN | NaN | NaN | 是 | 是 | 1.0 | 是 |
48851 | 441831 | 中午 | NaN | Phone | NaN | NaN | NaN | NaN | NaN | NaN | ... | NaN | NaN | 59.0 | NaN | NaN | NaN | 是 | 是 | 1.0 | 是 |
48852 | 1402044 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | 1.0 | ... | NaN | 1.0 | NaN | NaN | NaN | NaN | 否 | 否 | NaN | 否 |
48853 rows × 21 columns
4.10是否是复购用户(buy_again)
未购买的用户标记为‘未购买’,有购买未复购的用户标记为‘否’,有复购的用户标记为‘是’
#是否复购用户
buy_again = df[df['type']==4].groupby('customer_id')['product_id'].count().reset_index()
buy_again.rename(columns={'product_id':'buy_again'},inplace=True)
labels = pd.merge(labels,buy_again,how='left',on='customer_id')
#未购买的用户标记为‘未购买’,有购买未复购的用户标记为‘否’,有复购的用户标记为‘是’
labels['buy_again'] = labels['buy_again'].apply(lambda x: '是' if x>1 else '否' if x==1 else '未购买')
labels
#导出文件
labels.to_csv('JD_labels.csv')
customer_id | time_browse | time_Order | cate_most_browse | cate_most_Follow | cate_most_SavedCart | cate_most_Order | month_buy | month_Cart | month_active | week_buy | week_Cart | week_active | last_browse | last_SavedCart | last_Orger | interval_buy | browse_not_buy | cart_not_buy | buy_again | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 730494 | 下午 | NaN | Light | NaN | NaN | NaN | NaN | NaN | 1.0 | NaN | NaN | 1.0 | 4.0 | NaN | NaN | NaN | 是 | 是 | 未购买 |
1 | 1546231 | 上午 | NaN | Phone | NaN | NaN | NaN | NaN | NaN | 1.0 | NaN | NaN | 1.0 | 22.0 | NaN | NaN | NaN | 是 | 是 | 未购买 |
2 | 485903 | 中午 | NaN | Digital Camera | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | 69.0 | NaN | NaN | NaN | 是 | 是 | 未购买 |
3 | 1150917 | 晚上 | NaN | Candy | NaN | NaN | NaN | NaN | NaN | 1.0 | NaN | NaN | 1.0 | 13.0 | NaN | NaN | NaN | 是 | 是 | 未购买 |
4 | 992362 | 凌晨 | NaN | Digital Camera | NaN | NaN | NaN | NaN | NaN | 1.0 | NaN | NaN | 1.0 | 14.0 | NaN | NaN | NaN | 是 | 是 | 未购买 |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
48968 | 486260 | 凌晨 | NaN | Coat | NaN | NaN | NaN | NaN | NaN | 1.0 | NaN | NaN | 1.0 | 17.0 | NaN | NaN | NaN | 是 | 是 | 未购买 |
48969 | 1308135 | 下午 | NaN | Phone | NaN | NaN | NaN | NaN | NaN | 1.0 | NaN | NaN | 1.0 | 2.0 | NaN | NaN | NaN | 是 | 是 | 未购买 |
48970 | 658719 | 上午 | NaN | Phone | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | 70.0 | NaN | NaN | NaN | 是 | 是 | 未购买 |
48971 | 1465022 | 上午 | NaN | Phone | NaN | NaN | NaN | NaN | NaN | 1.0 | NaN | NaN | 1.0 | 26.0 | NaN | NaN | NaN | 是 | 是 | 未购买 |
48972 | 216989 | 晚上 | NaN | Tea | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | 70.0 | NaN | NaN | NaN | 是 | 是 | 未购买 |
48973 rows × 20 columns