You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python Pandas技术求助:计算高价书籍平均评分及分组可视化

Pandas 操作方案

1. 读取数据集

先把数据加载到Pandas DataFrame中(根据文件格式选择对应方法):

import pandas as pd
# CSV格式文件
df = pd.read_csv("你的数据集文件名.csv")
# 若为Excel格式,替换为以下代码
# df = pd.read_excel("你的数据集文件名.xlsx")

2. 计算高价书籍的平均评分

筛选出Price (Above Average)取值为Yes的行,直接计算对应评分列的平均值:

# 请将"User Rating"替换为你数据集中实际的评分列名
high_price_avg_rating = df[df["Price (Above Average)"] == "Yes"]["User Rating"].mean()
print(f"高价书籍的平均用户评分:{high_price_avg_rating:.2f}")

3. 获取两组平均评分(用于验证假设)

通过分组计算,同时得到高价、低价书籍的平均评分:

group_avg_rating = df.groupby("Price (Above Average)")["User Rating"].mean()
print(group_avg_rating)

4. 绘制分组平均评分图表

用Matplotlib生成可视化图表,直观对比两组评分差异:

import matplotlib.pyplot as plt

group_avg_rating.plot(kind="bar", color=["#2196F3", "#FFC107"])
plt.title("不同价格区间书籍的平均用户评分")
plt.xlabel("价格是否高于平均值")
plt.ylabel("平均用户评分")
plt.xticks(rotation=0)  # 保持x轴标签水平显示
plt.show()

注意事项

  • 确保代码中的列名与数据集里的完全一致(包括大小写、空格和符号),避免触发KeyError
  • 若数据集存在缺失值,可先通过df.dropna(subset=["Price (Above Average)", "User Rating"])清理无效行

内容的提问来源于stack exchange,提问作者Savrus

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 14:42:45