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

CSV数据加载至Bokeh失败,数据库加载正常,请求技术支持

Hey there! Let's work through this Bokeh loading issue step by step. Looking at your code, I spot a few key problems that are keeping your CSV-loaded data from rendering correctly:

Key Issues in Your Code

  • Index vs. Column Mix-Up: You set index_col='Year' which makes Year the DataFrame's index instead of a regular column. This breaks two things:
    1. The groupby('Year') call fails because Year isn't a column anymore.
    2. When creating ColumnDataSource(df), the index isn't included by default, so the x=index in your circle() call can't find that field.
  • Invalid x_range Value: You passed a groupby object to x_range, but Bokeh needs a list of categorical values (like your year labels) or a numeric range to render the axis properly.
  • Redundant Code: df = pd.DataFrame(df) is unnecessary—pd.read_csv() already returns a DataFrame.

Fixed Code Example

Here's a revised version that addresses all these issues:

import pandas as pd
from bokeh.plotting import figure, show, output_notebook
from bokeh.models import ColumnDataSource

# Load CSV, keep Year as a column (no need to set it as index here)
df = pd.read_csv('unemployment1948.csv', delimiter=',')

# Filter to the rows you want (35th row onwards)
filtered_df = df[35:]

# Create the data source with our filtered data
source = ColumnDataSource(filtered_df)

# Set x_range to the unique years from our filtered dataset
p = figure(plot_height=300, plot_width=900,
           x_range=filtered_df['Year'].unique().tolist(),
           title='Unemployment over the years',
           x_axis_label='Year', y_axis_label='Annual')

# Use the column names from the data source for x/y values
p.circle(x='Year', y='Annual', color='#35B778', source=source)

output_notebook()
show(p)

If You Need Year as an Index

If you rely on Year being the index for other operations, just reset it to a column before creating the data source:

df = pd.read_csv('unemployment1948.csv', delimiter=',', index_col='Year')
# Convert index back to a column
df = df.reset_index()
filtered_df = df[35:]

Quick Final Check

Double-check your CSV file to make sure the column names Year and Annual are spelled correctly (no extra spaces or typos)—that's a super common gotcha too!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.12 04:58:43