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

运行代码时持续触发TypeError: unhashable type: 'numpy.ndarray'求助

Fixing TypeError: unhashable type: 'numpy.ndarray' When Plotting Your Education Progress Data

Alright, let's figure out why you're hitting this frustrating error and get your plot working smoothly.

First, let's break down the issue: Your df_tertiary_total looks like a valid DataFrame with proper year indices and numeric percentage columns, so why is plt.plot() throwing this error? The root cause is that Matplotlib's base plot() function doesn't always play nicely with Pandas DataFrames when there's an unexpected data type in the index or columns—here, it’s likely your index got converted to a numpy array somewhere in your data processing pipeline.

Quick Fixes to Get Your Plot Working

1. Use Pandas' Built-in Plot Method (Easiest Solution)

Pandas has its own plot() method designed specifically for DataFrames, which handles index and column types seamlessly. Replace your last two lines of code with this:

# Use Pandas' native plot function
df_tertiary_total.plot(figsize=(10, 6))
plt.title('P1 Cohort Progress to Post-Secondary Education (2010-2019)')
plt.xlabel('Year')
plt.ylabel('Percentage')
plt.show()

This will automatically plot each column as a separate line, add a legend, and avoid the unhashable type error entirely.

2. Explicitly Specify X and Y in Matplotlib

If you prefer using Matplotlib directly, explicitly pass the index (your years) as the x-axis and each column as the y-axis. This removes any ambiguity in how Matplotlib processes the DataFrame:

plt.figure(figsize=(10, 6))
# Plot each group separately
plt.plot(df_tertiary_total.index, df_tertiary_total['Chinese'], label='Chinese')
plt.plot(df_tertiary_total.index, df_tertiary_total['Malay'], label='Malay')
plt.plot(df_tertiary_total.index, df_tertiary_total['Indian'], label='Indian')
plt.plot(df_tertiary_total.index, df_tertiary_total['Others'], label='Others')

# Add labels and legend
plt.legend()
plt.xlabel('Year')
plt.ylabel('Percentage')
plt.title('P1 Cohort Progress to Post-Secondary Education (2010-2019)')
plt.show()

3. Ensure Your Index is Hashable

Another possible fix is to make sure your DataFrame's index is a standard hashable type (like string or integer). Add this line before plotting:

# Convert index to string (or int if your years are stored as numbers)
df_tertiary_total.index = df_tertiary_total.index.astype(str)

This ensures Matplotlib can process the index without hitting the unhashable type error.

Bonus: Clean Up Your Code

You can also remove this redundant line from your code—df_tertiary_total is already a DataFrame after the concat operation, so converting it again is unnecessary:

# Remove this line, it's redundant
df_tertiary_total = pd.DataFrame(df_tertiary_total)

Any of these solutions should resolve the TypeError and get your plot showing up correctly.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.11 08:08:13