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 makesYearthe DataFrame's index instead of a regular column. This breaks two things:- The
groupby('Year')call fails becauseYearisn't a column anymore. - When creating
ColumnDataSource(df), the index isn't included by default, so thex=indexin yourcircle()call can't find that field.
- The
- Invalid
x_rangeValue: You passed a groupby object tox_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
相关产品推荐
相关产品推荐

