Search code examples
pythonpandascsvbokeh

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:

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:

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.

i hope someone can help me out, sorry for any bad English!


Solution

  • The x_range parameter in figure wants a list of strings if the x-axis is categorical. Just convert the years Series of integers to a Series of strings, modifying the row were you assign the variable years to:

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