[英]problems reading data from csv (Python)
im running in the problem and I have a hard time figuring out what it is so I'm here to ask for help.我遇到了这个问题,我很难弄清楚它是什么,所以我在这里寻求帮助。
when i try to run the following code:当我尝试运行以下代码时:
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)
with in the SampleData.csv these numbers:在 SampleData.csv 中这些数字:
year,amount
2010,1.5
2011,3
2012,5
2013,8
im getting the following error:我收到以下错误:
raise ValueError("Unrecognized range input: '%s'" % str(range_input))
ValueError: Unrecognized range input: '[2010 2011 2012 2013]'
the 2 print statements do give back the following numbers while running: 2 个打印语句在运行时会返回以下数字:
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
i know the problem is with the years = input["year"]
part because if i instead put years = ["2010","2010","2010","2010"]
it does work, and get the right data from numbers = input["amount"]
i dont see why that one is working but the years one is not.我知道问题在于years = input["year"]
部分,因为如果我改为输入years = ["2010","2010","2010","2010"]
它确实有效,并从中获取正确的数据numbers = input["amount"]
我不明白为什么那个人在工作,但几年没有。
i hope someone can help me out, sorry for any bad English!我希望有人可以帮助我,对不起,任何英语不好!
The x_range
parameter in figure
wants a list of strings if the x-axis is categorical.如果 x 轴是分类的, figure
的x_range
参数需要一个字符串列表。 Just convert the years
Series of integers to a Series of strings, modifying the row were you assign the variable years
to:只需将years
系列整数转换为一系列字符串,修改您将变量years
分配给的行:
years = input["year"].astype(str)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.