从 csv 读取数据时出现问题 (Python)

problems reading data from csv (Python)

我 运行 遇到了这个问题,我很难弄清楚它是什么,所以我来这里寻求帮助。

当我尝试 运行 以下代码时:

from bokeh.plotting import figure
from bokeh.io import export_svgs
from bokeh.plotting import figure, output_file, show
import pandas
import csv

output_file("bars.html")

input = pandas.read_csv("csv/SampleData.csv")

years = input["year"]
numbers = input["amount"]

print(years)
print(numbers)

p = figure(x_range=years, plot_height=250, title="graph title",
           toolbar_location=None, tools="")

p.vbar(x=years, top=numbers, width=0.9)

p.xgrid.grid_line_color = None
p.y_range.start = 0

show(p)

在 SampleData.csv 这些数字中:

year,amount
2010,1.5
2011,3
2012,5
2013,8

我收到以下错误:

raise ValueError("Unrecognized range input: '%s'" % str(range_input))
ValueError: Unrecognized range input: '[2010 2011 2012 2013]'

2 条打印语句在 运行ning:

时返回以下数字
0    2010
1    2011
2    2012
3    2013
Name: year, dtype: int64
0    1.5
1    3.0
2    5.0
3    8.0
Name: amount, dtype: float64

我知道问题出在 years = input["year"] 部分,因为如果我改为输入 years = ["2010","2010","2010","2010"] 它确实有效,并且从 numbers = input["amount"] 获得正确的数据我不明白为什么那个是工作,但几年没有。

我希望有人能帮助我, 抱歉英语不好!

如果 x 轴是分类的,figure 中的 x_range 参数需要一个字符串列表。只需将 years 系列整数转换为一系列字符串,修改行,将变量 years 分配给:

years = input["year"].astype(str)