为什么Bokeh Datatable中有Nan值?

时间:2018-02-20 13:24:59

标签: python jupyter-notebook bokeh

我编写了几个函数来根据输入Dataframe创建数据表。由于某种原因,值会丢失,输出只显示nan值。

from bokeh.models import ColumnDataSource, DataRange1d\
,Plot, LinearAxis, Grid,Label,LabelSet,HoverTool,NumeralTickFormatter
from bokeh.models.glyphs import HBar
from bokeh.io import curdoc, show, output_file
from bokeh.models.layouts import Column
from bokeh.resources import INLINE
import bokeh.io
bokeh.io.output_notebook() 
from bokeh.plotting import figure
from bokeh.sampledata.commits import data
from bokeh.transform import jitter
from bokeh.models.widgets import Panel, Tabs
from bokeh.layouts import widgetbox, Column
from bokeh.models.widgets import DataTable, DateFormatter, TableColumn,NumberFormatter,Panel, Tabs

def create_data_source(DF):
    columns = []
    DF = DF.reset_index().set_index('ServicedOn').T
    for item in DF:
        columns.append(TableColumn(field=items,
                                    title=item,
                                    width = 1000,
                                    formatter=NumberFormatter(format='0,0')))
    source = DF.drop('Year').to_dict(orient= 'list')
    return (source,columns)
def Create_data_table(DF,year):
        width = 1000
        source, columns = create_data_source(DF.loc[DF.index == str(year)])
        if 3 > len(source) < 7:
            width = 500
        elif len(source) < 3:
            width = 300
        Data_table = ( DataTable(columns = columns,source=ColumnDataSource(source),
                  height = 100, width = width,row_headers= False) )
        return (Panel(child=Data_table, title=str(year)))
show(Tabs(tabs = [Create_data_table(PD2,2016)]))

enter image description here Sample Dataframe就是这样的:

    ServicedOn  amount_spent
Year        
2016    October 31302
2016    November    5750
2016    December    0

我已经完成了代码,但无法理解值丢失的位置。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

您的代码有错误:itemS而不是item

# your code : columns.append(TableColumn(field=items, title=item [...]
columns.append(TableColumn(field=item, title=item [...]

也许是原因:散景无法找到数据