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

使用pandas绘制菜品评分柱状图报TypeError: unhashable type: 'list'求助

报错原因分析
  • 非法调用mean()方法:pandas中DataFrame.mean()的第一个参数为计算轴参数,仅支持整数、单轴名称类型,你传入列表['Dish']属于非法入参,是触发TypeError: unhashable type: 'list'的直接原因。
  • 数据结构被错误转换:连续调用.values属性会把原本的DataFrame结构转为numpy数组,而numpy数组没有pandas封装的plot()方法,即便修复传参问题也无法正常绘图。
  • 数据处理逻辑冗余:你的目标是按菜品分组计算平均评分,执行完groupby('Dish').mean()就已经得到了符合要求的结构化数据,后续多余的均值计算完全不需要。
修正后可运行代码
# -*- coding: utf-8 -*-
import pandas as pd
import matplotlib.pyplot as plt

# 读取数据集
df = pd.read_csv("https://gitlab.stud.iie.ntnu.no/nilstesd/python-big-data/raw/master/data/dishes.csv", sep=";", encoding="utf-8")
print(df.head())

# 按菜品分组计算平均评分,保留DataFrame结构即可
df = df.groupby(['Dish'])['Score'].mean()

plt.figure(figsize = (8,6), dpi = 72)
plt.xlabel('Dish')
plt.ylabel('Score')
plt.title('Dishes')
ax = plt.gca()
colors= ['r', 'b']
df.plot(kind='bar',color=colors, alpha=0.75, ax=ax)
plt.grid(b=True, which='major', linewidth=1.0)
plt.grid(b=True, which='minor')
plt.show()
关键修改说明
  • 删除多余的两次mean()调用和.values取值操作,保留groupby后的结构化数据供绘图调用
  • 明确指定groupby后仅提取Score列计算均值,避免生成多余的索引层级

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.27 16:36:02