[英]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 個打印語句在運行時會返回以下數字:
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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.