数据分析之实战项目——电商用户行为分析【python】


注:回顾,整理,扩充之前做的;下一步准备用SQL进行分许。

1、项目背景和目的

\quad \quad 随着互联网和电商的发展,人们习惯于网上购物。在国内,电商平台深受欢迎,每年的双11,双12活动,大量的用户在淘宝平台浏览商品,或收藏或加入购物车或直接购买。通过对用户的行为分析,探索用户购买的规律,了解商品的受欢迎程度,结合店铺的营销策略,实现更加精细和精准的运营,让业务获得更好的增长。

2、数据集简介

\quad \quad 本数据集来源:天池淘宝数据

\quad \quad 本数据集包含了淘宝11、12两月份的用户行为数据,(行为包含点击、购买、加购、喜欢)。每一行表示一条用户行为,由用户ID、商品ID、商品类目ID、行为类型和时间戳、用户位置组成。

  • user_id:整数类型,序列化后的用户ID;

  • item_id:整数类型,序列化后的商品ID;

  • behavior_type:字符串,包括(“pv”:相当于点击,“buy”:商品购买,“cart”:将商品加入购物车,“fav”:收藏商品)

  • user_geohash:纬度(行为发生时的用户位置,可以为空)

  • item_category:商品类别

  • time:行为发生的时间戳

3、数据处理

3.1 加载数据

导入相关库

import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from datetime import datetime

1、此数据为zip形式,先解压至当前目前目录

# 1、解压文件
# -*-coding:utf-8 -*-
import os
import zipfile
import shutil
def unzipDir(path):
    """解压"""
    zip_file = zipfile.ZipFile(path)
    try:
        zip_file.extractall(os.path.split(path)[0]) # 解压到当前目录
    except Exception as e:
        raise e
path='E:/tianchi_mobile_recommend_train_user.zip'
unzipDir(path)

2、读取数据

user_action=pd.read_csv('E:/tianchi_mobile_recommend_train_user.csv')
print(user_action.shape)

output:

(12256906, 6)

3、由于数据量太大,降低分析效率,故本次分析随机选取百分之5的数据(存放到一文件中)进行分析。

# random select 5% from dataset
sample = user_action.sample(frac=0.05, random_state=5, axis=0)
# export to csv file
sample.to_csv('E:/Userdata.csv',encoding='utf_8_sig')
user_data=pd.read_csv('E:/Userdata.csv')

3.2 数据概览

1、数据集大小、首尾数据

print(user_data.shape)
print(user_data.head().append(user_data.tail()))

Output:

(612845, 7)
         (612845, 7)
        Unnamed: 0    user_id    item_id  behavior_type user_geohash  \
0          1165106   21660809  286436627              1          NaN   
1          4884642  127429787  129402528              1          NaN   
2          3706587   37780288  261186825              1          NaN   
3          4148620   62969316  141908693              1      95qsrjo   
4          5801818   15237890  195559483              1          NaN   
612840     5299504  107109917   66920889              1      954f4l7   
612841     6036343   56210078  257707426              1          NaN   
612842    10363581  129809988  400224753              1      9q9g00w   
612843     4975578   81904568   51594957              1          NaN   
612844     5938559   45966877  210330569              1      95q6qed   

        item_category           time  
0                5263  2014-12-02 00  
1               13547  2014-12-04 22  
2                4370  2014-12-08 17  
3               12147  2014-12-12 21  
4                4116  2014-12-14 07  
612840           4640  2014-12-08 21  
612841          10079  2014-12-14 15  
612842          11623  2014-12-17 23  
612843           1863  2014-12-05 12  
612844           2883  2014-12-13 21  

2、基本信息

user_data.info()

Output:

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 612845 entries, 0 to 612844
Data columns (total 7 columns):
 #   Column         Non-Null Count   Dtype 
---  ------         --------------   ----- 
 0   Unnamed: 0     612845 non-null  int64 
 1   user_id        612845 non-null  int64 
 2   item_id        612845 non-null  int64 
 3   behavior_type  612845 non-null  int64 
 4   user_geohash   196204 non-null  object
 5   item_category  612845 non-null  int64 
 6   time           612845 non-null  object
dtypes: int64(5), object(2)
memory usage: 32.7+ MB
  • Unnamed: 0这一特征是随机采样产生的特征,无用,可将这一列删除
  • 经观察,只有user_geohash即用户位置有大量的缺失值,而且这一特征分析也没有意义,故也将其特征删除

3.3 数据清洗

1、删除无用数据列

#删除无用数据列
del user_data['user_geohash']
del user_data['Unnamed: 0']
user_data.columns

Output:

Index(['user_id', 'item_id', 'behavior_type', 'item_category', 'time'], dtype='object')

2、处理时间戳

#把数据自带的字符时间转换成datetime对象,可以更好的拿出需要的时间段
time = pd.to_datetime(user_data['time'])
user_data['hour'] = time.dt.hour
user_data['weekday'] = time.dt.weekday
user_data['date'] = time.dt.date

3、将用户行为标签由数字类型改为用字符表示

# 把用户行为对应成相对应的pv collect buy
behavior = {
   1:'pv',2:'collect',3:'cart',4:'buy'}
user_data['behavior_type'] = user_data['behavior_type'].apply(lambda x: behavior[x])
user_data.head()

Output:

在这里插入图片描述

4、数据分析

分析大纲
在这里插入图片描述

4.1 电商数据分析

4.1.1 流量分析

4.1.1.1 总体流量分析
# 总体访问量,收藏,加购,下单
total_liu=user_data.groupby(['behavior_type']).size()
pv=total_liu[3]
# 总访客数
uv=user_data['user_id'].nunique()
# 消费用户数
user_pay=user_data[user_data['behavior_type']=='buy']['user_id'].unique()
# 日均访问量
pv_per_day=pv/user_data['date'].nunique()
# 人均访问量
pv_per_user=pv/uv
# 消费用户访问量
pv_pay=user_data[user_data['user_id'].isin(user_pay)]['behavior_type'].value_counts().pv
# 消费用户数占比
user_pay_rate=len(user_pay)/uv
# 消费用户访问量占比
pv_pay_rate=pv_pay/pv
# 消费用户人均访问量
pv_per_buy_user=pv_pay/len(user_pay)
print('总访问量为',total_liu)
print('总访客数为 %i'%uv)
print('消费用户数为 %i'%len(user_pay))
print('消费用户访问量为 %i'%pv_pay)
print('日均访问量为 %.3f'%pv_per_day)
print('人均访问量为 %.3f'%pv_per_user)
print('消费用户人均访问量为 %.3f'%pv_per_buy_user)
print('消费用户数占比为 %.3f%%'%(user_pay_rate * 100))
print('消费用户访问量占比为 %.3f%%'%(pv_pay_rate * 100))

Output:

总访问量为 behavior_type
buy          6053
cart        17224
collect     12054
pv         577514
dtype: int64
总访客数为 9868
消费用户数为 3530
消费用户访问量为 300533
日均访问量为 18629.484
人均访问量为 58.524
消费用户人均访问量为 85.137
消费用户数占比为 35.772%
消费用户访问量占比为 52.039%
4.1.1.2 每日流量分析
# 每日流量
# 每日PV,收藏,加购,下单
#unstack() 这是一个根据索引行列转换的函数
day_liu=user_data.groupby(['date','behavior_type']).size().unstack()
print(day_liu)
# 每日uv
dayily_uv=user_data.groupby('date')['user_id'].apply(lambda x:x.drop_duplicates().count()).reset_index().rename(columns={
   'user_id':'uv'})
print(dayily_uv)
behavior_type  buy  cart  collect     pv
date                                    
2014-11-18     202   540      349  17312
2014-11-19     171   508      360  17149
2014-11-20     168   493      351  16476
2014-11-21     154   416      313  15686
2014-11-22     166   497      331  16879
2014-11-23     193   535      357  18099
2014-11-24     167   533      374  17671
2014-11-25     188   486      352  17364
2014-11-26     175   452      364  17121
2014-11-27     155   482      388  17438
2014-11-28     160   439      311  15960
2014-11-29     179   526      359  17132
2014-11-30     198   569      397  19146
2014-12-01     201   560      371  18457
2014-12-02     168   533      389  19045
2014-12-03     205   568      388  19392
2014-12-04     197   602      422  18796
2014-12-05     165   487      337  16911
2014-12-06     162   502      435  18469
2014-12-07     167   593      472  18858
2014-12-08     156   557      433  18254
2014-12-09     179   565      432  18685
2014-12-10     175   656      437  19967
2014-12-11     168   782      455  23039
2014-12-12     723  1202      519  32229
2014-12-13     176   525      407  19301
2014-12-14     199   539      390  19332
2014-12-15     196   502      445  19075
2014-12-16     193   529      372  18587
2014-12-17     188   524      377  18130
2014-12-18     159   522      367  17554
          date    uv
0   2014-11-18  4235
1   2014-11-19  4261
2   2014-11-20  4193
3   2014-11-21  4041
4   2014-11-22  4101
5   2014-11-23  4302
6   2014-11-24  4313
7   2014-11-25  4166
8   2014-11-26  4255
9   2014-11-27  4210
10  2014-11-28  4071
11  2014-11
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值