Bokeh: Column DataSource part giving error

Viewed 5407

I am trying to create an interactive bokeh plot that holds multiple data and I am not sure why I am getting the error

ValueError: expected an element of ColumnData(String, Seq(Any)),got {'x': 6.794, 'y': 46.8339999999999, 'country': 'Congo, Dem. Rep.', 'pop': 3.5083789999999997, 'region': 'Sub-Saharan Africa'}

source = ColumnDataSource(data={
'x'       : data.loc[1970].fertility,
'y'       : data.loc[1970].life,
'pop'     : (data.loc[1970].population / 20000000) + 2,
'region'  : data.loc[1970].region,

})

I have tried two different data sets by importing data from excel and have been running out of issues on exactly why this happening.

enter image description here

4 Answers

As the name suggests, the ColumnDataSource is a data structure for storing columns of data. This means that the value of every key in .data must be a column, i.e. a Python list, a NumPy array, or a Pandas series. But you are trying to assign plain numbers as the values, which is what the error message is telling you:

I am trying to create an interactive bokeh plot that holds multiple data and I am not sure why I am getting the error

expected an element of ColumnData(String, Seq(Any))

This is saying acceptable, expected values are dicts that map strings to sequences. But what you passed is clearly not that:

got {'x': 6.794, 'y': 46.8339999999999, 'country': 'Congo, Dem. Rep.', 'pop': 3.5083789999999997, 'region': 'Sub-Saharan Africa'}

The value for x for instance is just the number 6.794 and not an array or list, etc.

You can easily do this:

source = ColumnDataSource({str(c): v.values for c, v in df.items()})

This would be a solution. I think the problem is in getting the data from df.

source = ColumnDataSource(data={
    'x'       : data[data['Year'] == 1970]['fertility'],
    'y'       : data[data['Year'] == 1970]['life'],
    'pop'     : (data[data['Year'] == 1970]['population']/20000000) + 2,
    'region'  : data[data['Year'] == 1970]['region']
})

I had this same problem using this same dataset. My solution was to import the csv in pandas using "Year" as index column.

data = pd.read_csv(csv_path,  index_col='Year')
Related