Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the source of the index in a ColumnDataSource created from a pandas DataFrame?

Tags:

python

bokeh

Dataframes naturally come with an index, i.e. those set of row headers we can think of them as. When I construct a ColumnDataSource in bokeh to capture the information in that dataframe for plotting and annotating in a HoverTool I see that the hover tool has a built in ( "index" , "$index" ) tooltip available. Will this index be identical to my dataframe's index or is it simply the row index in the ColumnDataSource

like image 389
jxramos Avatar asked Oct 18 '25 14:10

jxramos


1 Answers

The special variable $index simply displays the row index of the column data source (it can't be a pandas index in general, because although CDS may be created from data frames, they do not have to). If you want to include the pandas dataframe index you can add it:

In [5]: d = pd.DataFrame(dict(a=[1,2,3], b=[2,3,4]))

In [6]: d.index
Out[6]: RangeIndex(start=0, stop=3, step=1)

In [7]: source = ColumnDataSource(d)

In [8]: source.add(d.index, 'index')

This field can be accessed in a hover tool with the standard and general @colname syntax for any standard CDS column (so in this specific case: @index)

like image 176
bigreddot Avatar answered Oct 21 '25 05:10

bigreddot



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!