第五篇: 使用Python和BigQuery进行电商数据分析与可视化

使用Python和BigQuery进行电商数据分析与可视化

大数据分析对于电商业务的洞察至关重要。在这篇文章中,我们将使用Python结合Google BigQuery来分析电商数据集,以最畅销商品平均订单价格最高的前10位客户为主题,展示如何通过数据可视化提供有价值的业务见解。我们将重点介绍数据提取和可视化,帮助读者掌握在实际场景中如何直观展示数据分析结果。

1. 数据集与分析目标

本文使用Google BigQuery提供的公共电商数据集bigquery-public-data:thelook_ecommerce,其中包含用户的购买行为和商品信息等数据。本文的主要分析目标有两个:

  • 最畅销商品:分析销售量排名最高的商品。
  • 平均订单价格最高的前10位客户:识别在平均订单价格上消费较高的客户,以支持客户关系管理。

2. 设置环境并连接BigQuery

首先,确保安装了BigQuery的Python客户端库以及Matplotlib和Seaborn数据可视化库:

bash 复制代码
pip install google-cloud-bigquery matplotlib seaborn

初始化BigQuery客户端并进行身份验证:

python 复制代码
from google.cloud import bigquery

# 初始化BigQuery客户端
client = bigquery.Client()

3. 查询并获取数据

接下来,我们使用SQL查询BigQuery中的数据,提取最畅销商品和平均订单价格最高的前10位客户的数据。

查询1:最畅销商品

计算订单中每个商品的销量,并按销量从高到低排序:

sql 复制代码
SELECT 
    oi.product_id AS product_id, 
    p.name AS product_name, 
    p.category AS product_category, 
    COUNT(*) AS num_of_orders 
FROM 
    `bigquery-public-data.thelook_ecommerce.products` AS p 
JOIN 
    `bigquery-public-data.thelook_ecommerce.order_items` AS oi 
ON 
    p.id = oi.product_id 
GROUP BY 
    1, 2, 3 
ORDER BY 
    num_of_orders DESC;

在Python中执行该查询:

python 复制代码
# 查询最畅销商品
best_selling_query = """
SELECT 
    oi.product_id AS product_id, 
    p.name AS product_name, 
    p.category AS product_category, 
    COUNT(*) AS num_of_orders 
FROM 
    `bigquery-public-data.thelook_ecommerce.products` AS p 
JOIN 
    `bigquery-public-data.thelook_ecommerce.order_items` AS oi 
ON 
    p.id = oi.product_id 
GROUP BY 
    1, 2, 3 
ORDER BY 
    num_of_orders DESC;
"""
best_selling_items = client.query(best_selling_query).to_dataframe()
查询2:平均订单价格最高的前10位客户

获取每个客户的平均订单价格,并按降序排列以显示消费水平最高的10位客户:

sql 复制代码
SELECT 
    u.id AS user_id, 
    u.first_name, 
    u.last_name, 
    AVG(oi.sale_price) AS avg_sale_price 
FROM 
    `bigquery-public-data.thelook_ecommerce.users` AS u 
JOIN 
    `bigquery-public-data.thelook_ecommerce.order_items` AS oi 
ON 
    u.id = oi.user_id 
GROUP BY 
    1, 2, 3 
ORDER BY 
    avg_sale_price DESC 
LIMIT 10;

在Python中执行查询:

python 复制代码
# 查询平均订单价格最高的前10位客户
top_customers_query = """
SELECT 
    u.id AS user_id, 
    u.first_name, 
    u.last_name, 
    AVG(oi.sale_price) AS avg_sale_price 
FROM 
    `bigquery-public-data.thelook_ecommerce.users` AS u 
JOIN 
    `bigquery-public-data.thelook_ecommerce.order_items` AS oi 
ON 
    u.id = oi.user_id 
GROUP BY 
    1, 2, 3 
ORDER BY 
    avg_sale_price DESC 
LIMIT 10;
"""
top_customers = client.query(top_customers_query).to_dataframe()

4. 数据可视化

获取数据后,接下来用Matplotlib和Seaborn将结果进行可视化。

4.1 最畅销商品柱状图

将最畅销商品按销量绘制成柱状图:

python 复制代码
import matplotlib.pyplot as plt
import seaborn as sns

# 绘制最畅销商品的柱状图
plt.figure(figsize=(10, 6))
sns.barplot(x='num_of_orders', y='product_name', data=best_selling_items.head(10), palette='viridis')
plt.title('Top 10 Best Selling Products')
plt.xlabel('Number of Orders')
plt.ylabel('Product Name')
plt.yticks(rotation=45, fontsize=8)  # 旋转45度并缩小字体
plt.show()

代码绘制图表示例:

4.2 平均订单价格最高的前10位客户饼图

将前10位客户的平均订单价格按比例绘制成饼图,展示客户消费贡献情况。

python 复制代码
# 绘制平均订单价格最高的前10位客户饼图
labels = top_customers['first_name'] + ' ' + top_customers['last_name']
sizes = top_customers['avg_sale_price']

plt.figure(figsize=(8, 8))
plt.pie(sizes, labels=labels, autopct='%1.1f%%', startangle=140)
plt.title('Top 10 Customers by Average Order Price')
plt.show()

代码绘制图表示例:

5. 分析结果

  • 最畅销商品:通过柱状图展示商品销量,便于识别出在市场中更受欢迎的产品,有助于制定营销策略。
  • 前10位客户的平均订单价格:饼图显示这些高消费客户的相对贡献,可以为建立客户关系和激励措施提供支持。

总结

本文介绍了如何使用Python和Google BigQuery进行电商数据分析,展示了从数据提取到结果可视化的完整流程。在电商数据分析中,数据的可视化帮助我们更快识别业务趋势,未来还可以拓展至更复杂的分析需求,例如客户分层、产品推荐等。

相关推荐
ζั͡山 ั͡有扶苏 ั͡✾1 小时前
从零搭建 Data-Juicer:一站式大模型数据预处理与可视化平台完整教程
python·data-juicer
TTBIGDATA1 小时前
【Ambari开启Kerberos】KERBEROS SERVICE CHECK 报错
大数据·运维·hadoop·ambari·cdh·bigtop·ttbigdata
SkylerHu1 小时前
tornado+gunicorn部署设置max_body_size
python·tornado·gunicorn
开利网络1 小时前
合规底线:健康产品营销的红线与避坑指南
大数据·前端·人工智能·云计算·1024程序员节
非著名架构师2 小时前
量化“天气风险”:金融与保险机构如何利用气候大数据实现精准定价与投资决策
大数据·人工智能·新能源风光提高精度·疾风气象大模型4.0
Hello.Reader2 小时前
用 CdcUp CLI 一键搭好 Flink CDC 演练环境
大数据·flink
独行soc2 小时前
2025年渗透测试面试题总结-234(题目+回答)
网络·python·安全·web安全·渗透测试·1024程序员节·安全狮
木头左2 小时前
年化波动率匹配原则在ETF网格区间选择中的应用
python
清空mega2 小时前
从零开始搭建 flask 博客实验(3)
后端·python·flask
熙梦数字化3 小时前
2025汽车零部件行业数字化转型落地方案
大数据·人工智能·汽车