Home > Article > Backend Development > Pandas+Pyecharts | Electronic product sales data analysis visualization + user RFM portrait
This issue uses python to analyze a electronic product sales data, take a look :
##Monthly order quantity order amount
Daily order quantity distribution
##Male and female users Order ratio
Female/Male Purchase Products TOP20
Order quantity for each age group
User RFM level portrait
##Wait...
Involved libraries:
Pandas
—Data processingPyecharts
—Data Visualizationimport pandas as pd
from pyecharts.charts import Line
from pyecharts.charts import Bar
from pyecharts.charts import Pie
from pyecharts.charts import Grid
from pyecharts.charts import PictorialBar
from pyecharts import options as opts
from pyecharts.commons.utils import JsCode
import warnings
warnings.filterwarnings('ignore')
df = pd.read_csv("电子产品销售分析.csv")
df.info()
一共有564169条数据,其中category_code、brand两列有部分数据缺失。
2.3 去掉部分用不到的列
df1 = df[['event_time', 'order_id', 'category_code', 'brand', 'price', 'user_id', 'age', 'sex', 'local']] df1.shape
(564169, 9)
2.4 去除重复数据
df1 = df1.drop_duplicates() df1.shape
(556456, 9)
2.5 增加部分时间列
df1['event_time'] = pd.to_datetime(df1['event_time'].str[:19],format="%Y-%m-%d %H:%M:%S") df1['Year'] = df1['event_time'].dt.year df1['Month'] = df1['event_time'].dt.month df1['Day'] = df1['event_time'].dt.day df1['hour'] = df1['event_time'].dt.hour df1.head(10)
2.6 过滤数据,也可以选择均值填充
df1 = df1.dropna(subset=['category_code']) df1 = df1[(df1["Year"] == 2020)&(df1["price"] > 0)] df1.shape
(429261, 13)
2.7 对年龄分组
df1['age_group'] = pd.cut(df1['age'],[10,20,30,40,50],labels=['10-20','20-30','30-40','40-50'])
2.8 增加商品一、二级分类
df1["category_code_1"] = df1["category_code"].apply(lambda x: x.split(".")[0] if "." in x else x) df1["category_code_2"] = df1["category_code"].apply(lambda x: x.split(".")[-1] if "." in x else x) df1.head(10)
def get_bar1(): bar1 = ( Bar() .add_xaxis(x_data) .add_yaxis("订单数量", y_data1) .extend_axis(yaxis=opts.AxisOpts(axislabel_opts=opts.LabelOpts(formatter="{value}万"))) .set_global_opts( legend_opts=opts.LegendOpts(pos_top='25%', pos_left='center'), title_opts=opts.TitleOpts( title='1-每月订单数量订单额', subtitle='-- 制图@公众号:Python当打之年 --', pos_top='7%', pos_left="center" ) ) ) line = ( Line() .add_xaxis(x_data) .add_yaxis("订单额", y_data2, yaxis_index=1) ) bar1.overlap(line)
8月份的订单量和订单额达到峰值。
def get_bar2(): pie1 = ( Pie() .add( "", datas, radius=["13%", "25%"], label_opts=opts.LabelOpts(formatter="{b}: {d}%"), ) ) bar1 = ( Bar(init_opts=opts.InitOpts(theme='dark', width='1000px', height='600px', bg_color='#0d0735')) .add_xaxis(x_data) .add_yaxis("", y_data, itemstyle_opts=opts.ItemStyleOpts(color=JsCode(color_function))) .set_global_opts( legend_opts=opts.LegendOpts(is_show=False), title_opts=opts.TitleOpts( title='2-一月各天订单数量分布', subtitle='-- 制图@公众号:Python当打之年 --', pos_top='7%', pos_left="center" ) ) ) bar1.overlap(pie1)
男性订单数量占比49.55%,女性订单数量占比50.45%,基本持平。
3.5 女性/男性购买商品TOP20
def get_bar3(): bar1 = ( Bar() .add_xaxis(x_data1) .add_yaxis('女性', y_data1, label_opts=opts.LabelOpts(position='right') ) .set_global_opts( title_opts=opts.TitleOpts( title='5-女性/男性购买商品TOP20', subtitle='-- 制图@公众号:Python当打之年 --', pos_top='3%', pos_left="center"), legend_opts=opts.LegendOpts(pos_left='20%', pos_top='10%') ) .reversal_axis() ) bar2 = ( Bar() .add_xaxis(x_data2) .add_yaxis('男性', y_data2, label_opts=opts.LabelOpts(position='right') ) .set_global_opts( legend_opts=opts.LegendOpts(pos_right='25%', pos_top='10%') ) .reversal_axis() ) grid1 = ( Grid() .add(bar1, grid_opts=opts.GridOpts(pos_left='12%', pos_right='50%', pos_top='15%')) .add(bar2, grid_opts=opts.GridOpts(pos_left='60%', pos_right='5%', pos_top='15%')) )
3.7 各年龄段购买商品TOP10
3.8 用户RFM等级画像
RFM模型是衡量客户价值和客户创利能力的重要工具和手段。该模型通过一个客户的近期购买行为(R)、购买的总体频率(F)以及花了多少钱(M)三项指标来描述该客户的价值状况,从而能够更加准确地将成本和精力更精确的花在用户层次身上,实现针对性的营销。
用户分类:
def rfm_func(x): level = x.apply(lambda x:"1" if x > 0 else '0') RMF = level.R + level.F + level.M dic_rfm ={ '111':'重要价值客户', '011':'重要保持客户', '101':'重要发展客户', '001':'重要挽留客户', '110':'一般价值客户', '100':'一般发展客户', '010':'一般保持客户', '000':'一般挽留客户' } result = dic_rfm[RMF] return result
计算等级:
df_rfm = df1.copy() df_rfm = df_rfm[['user_id','event_time','price']] # 时间以当年年底为准 df_rfm['days'] = (pd.to_datetime("2020-12-31")-df_rfm["event_time"]).dt.days # 计算等级 df_rfm = pd.pivot_table(df_rfm,index="user_id", values=["user_id","days","price"], aggfunc={"user_id":"count","days":"min","price":"sum"}) df_rfm = df_rfm[["days","user_id","price"]] df_rfm.columns = ["R","F","M"] df_rfm['RMF'] = df_rfm[['R','F','M']].apply(lambda x:x-x.mean()).apply(rfm_func,axis=1) df_rfm.head()
用户画像:
根据RFM模型可将用户分为以下8类:
重要保持客户:最近消费时间较远,消费金额和频次都很高。
Important development customers: The recent consumption time is relatively recent and the consumption amount is high, but the frequency is not high and the loyalty is not high, very Potential users must be developed with emphasis.
Important Customer Retention: Those who recently spent a lot of time and the frequency of consumption is not high, but the amount of consumption is high Users, who may be about to be lost or have already been lost, should be given retention measures.
General value customers: recent consumption time, high frequency but low consumption amount. Need to improve their customers unit price.
General development customers: The recent consumption time is relatively recent, and the consumption amount and frequency are not high.
Generally retain customers: The recent consumption time is far away, the consumption frequency is high, and the consumption amount is not high.
The above is the detailed content of Pandas+Pyecharts | Electronic product sales data analysis visualization + user RFM portrait. For more information, please follow other related articles on the PHP Chinese website!