Travel Tips
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
PYTHON实战——用户消费分析一
import matplotlib.pyplot as plt import numpy as np import pandas as pd
'''
user_id:用户id
order_dt:购买日期
order_product:购买产品数
order_amount:购买金额
'''
column = ['user_id', 'order_dt', 'order_product', 'order_amount'] txt = pd.read_csv('cd.txt', sep='\s+', names=column) print(txt.head())
print(txt.info())
print(txt.describe())
txt['order_dt'] = pd.to_datetime(txt.order_dt, format='%Y%m%d') txt['month'] = txt.order_dt.values.astype('datetime64[M]') print(txt.head())
'''
用户消费趋势分析
每月的消费总金额
每月消费次数
每月产品购买量
每月消费人数
'''
group_month = txt.groupby('month') order_month_amount = group_month.order_amount.sum() print(order_month_amount.head())
plt.style.use('ggplot') order_month_amount.plot() plt.show() # 前三个月较高
group_month.user_id.count().plot() plt.show()
group_month.order_amount.sum().plot() plt.show()
txt.groupby('month').user_id.apply(lambda x: len(x.drop_duplicates())).plot() plt.show()
pivot = txt.pivot_table(index='month', values=['order_product', 'order_amount', 'user_id'], aggfunc={'order_product': 'sum', 'order_amount': 'sum', 'user_id': 'count'}) print(pivot.head())
pivot.plot() plt.show()
'''
用户个体消费分析
用户消费金额、消费次数的描述统计
用户消费金额和消费的散点图
用户消费金额的分布图
用户消费次数的分布图
用户累积消费的占比
'''
group_user = txt.groupby('user_id') print(group_user.sum().describe()) # 用户平均购买了七张CD,但中位值只有3,说明小部分购买了大量CD # 用户平均消费了106元,中位值有43,有极值干扰
group_user.sum().plot.scatter(x='order_amount', y='order_product') plt.show() # 可以从中看出有极值要过滤
group_user.sum().query('order_amount<4000').plot.scatter(x='order_amount', y='order_product') plt.show()
group_user.sum().order_amount.plot.hist(bins=20) plt.show()
group_user.sum().query('order_amount < 1000').order_amount.plot.hist(bins=40) plt.show()
user_cumsum = group_user.sum().sort_values('order_amount').apply(lambda x: x.cumsum() / x.sum()) print(user_cumsum)
user_cumsum.reset_index().order_amount.plot() plt.show() # 按用户消费金额进行升序排列,由图知50%的用户贡献了15%的消费度而排名前5000的用户就贡献了60%的消费
Sed ac lorem felis. Ut in odio lorem. Quisque magna dui, maximus ut commodo sed, vestibulum ac nibh. Aenean a tortor in sem tempus auctor
December 4, 2020 at 3:12 pm
Sed ac lorem felis. Ut in odio lorem. Quisque magna dui, maximus ut commodo sed, vestibulum ac nibh. Aenean a tortor in sem tempus auctor
December 4, 2020 at 3:12 pm
Donec in ullamcorper quam. Aenean vel nibh eu magna gravida fermentum. Praesent eget nisi pulvinar, sollicitudin eros vitae, tristique odio.
December 4, 2020 at 3:12 pm
我是 s enim interduante quis metus. Duis porta ornare nulla ut bibendum
Rosie
6 minutes ago