第五篇: 使用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进行电商数据分析,展示了从数据提取到结果可视化的完整流程。在电商数据分析中,数据的可视化帮助我们更快识别业务趋势,未来还可以拓展至更复杂的分析需求,例如客户分层、产品推荐等。

相关推荐
189228048611 小时前
NW728NW733美光固态闪存NW745NW746
大数据·服务器·网络·人工智能·性能优化
你怎么知道我是队长2 小时前
python-enumrate函数
开发语言·chrome·python
大熋3 小时前
Playwright Python 教程:网页自动化
开发语言·python·自动化
A7bert7773 小时前
【YOLOv8-obb部署至RK3588】模型训练→转换RKNN→开发板部署
linux·c++·人工智能·python·yolo
cdg==吃蛋糕3 小时前
selenium 使用方法
开发语言·python
Y1nhl4 小时前
力扣_二叉树的BFS_python版本
python·算法·leetcode·职场和发展·宽度优先
Q_Q5110082855 小时前
python的婚纱影楼管理系统
开发语言·spring boot·python·django·flask·node.js·php
若兰幽竹5 小时前
【从零开始编写数据库:基于Python语言实现数据库ToyDB的ACID特性】
数据库·python
xiaocainiao8815 小时前
Python 实战:构建 Git 自动化助手
git·python·自动化
云朵大王5 小时前
SQL 视图与事务知识点详解及练习题
java·大数据·数据库