调整散景中的滑块后,为什么包含度量名称的TableColumn消失了?

时间:2018-09-12 16:18:55

标签: python python-3.x pandas bokeh

我已经基于.describe()的输出创建了具有bokeh的DataTable,该散景在我调整滑块时会更新。数据表中的所有其他列似乎都在正确更新,但是包含度量名称的第一列在回调时会消失。也就是说,标题仍然存在,但是该字段为空白。如何解决此问题,以使度量值列不会空白?

我的代码如下:

def modify_table(doc):

    my_cols = ['Tuition Per Student', 'Students', 'Total Tuition', 'Other Revenue', 'Total Revenue', 'Expense', 'Margin']

    source = ColumnDataSource(ed_montecarlo(department_data, 'Education and Early Childhood', num=1000, expense_var=.3)[my_cols]. \
                          describe().iloc[1:,].round(2))

    columns = [TableColumn(field='index', title='Measure'), 
               TableColumn(field='Tuition Per Student', title='Tuition Per Student'),
               TableColumn(field='Students', title='Number of Students'),
               TableColumn(field='Total Tuition', title='Total Tuition'),
               TableColumn(field='Other Revenue', title='Other Revenue'),
               TableColumn(field='Total Revenue', title='Total Revenue'),
               TableColumn(field='Expense', title='Total Expense'),
               TableColumn(field='Margin', title='Margin')]

    data_table = DataTable(source=source, columns=columns, index_position=None, width=700)

    def callback(attr, old, new):
        other_sims = ed_montecarlo(department_data, 'Education and Early Childhood', num=iter_slider.value, expense_var=.3)[my_cols]
        other_sims_table = other_sims.describe().iloc[1:,].round(2)

        source.data = {
            'Measure'                : list(other_sims_table.index),
            'Tuition Per Student'    : list(other_sims_table['Tuition Per Student']),
            'Students'               : list(other_sims_table['Students']),
            'Total Tuition'          : list(other_sims_table['Total Tuition']),
            'Other Revenue'          : list(other_sims_table['Other Revenue']),
            'Total Revenue'          : list(other_sims_table['Total Revenue']),
            'Expense'                : list(other_sims_table['Expense']),
            'Margin'                 : list(other_sims_table['Margin']),

    }

    iter_slider = Slider(start=100, end=5000, step=100, value=1000, title='Number of Iterations')
    iter_slider.on_change('value', callback) 
    widg = widgetbox(iter_slider)

    doc.add_root(column(widg, data_table))

show(modify_table)     

请注意,ed_montecarlo是我编写的函数,该函数返回蒙特卡洛模拟的不同迭代的数据帧输出。除了这一栏外,其他所有内容似乎都正常运行。

这是我调整滑块之前的输出: Before Adjusting Slider

这是以下内容的输出: enter image description here

任何人和所有帮助将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:0)

我找到了答案。问题出在这个块上:

    source.data = {
        'Measure'                : list(other_sims_table.index),
        'Tuition Per Student'    : list(other_sims_table['Tuition Per Student']),
        'Students'               : list(other_sims_table['Students']),
        'Total Tuition'          : list(other_sims_table['Total Tuition']),
        'Other Revenue'          : list(other_sims_table['Other Revenue']),
        'Total Revenue'          : list(other_sims_table['Total Revenue']),
        'Expense'                : list(other_sims_table['Expense']),
        'Margin'                 : list(other_sims_table['Margin']),

}

我应该改为将“ Measure”更改为“ index”,结果是我创建了一个名为“ Measure”的新列,该列随后未在主函数中被引用。正确的代码是:

    source.data = {
        'index'                  : list(other_sims_table.index),
        'Tuition Per Student'    : list(other_sims_table['Tuition Per Student']),
        'Students'               : list(other_sims_table['Students']),
        'Total Tuition'          : list(other_sims_table['Total Tuition']),
        'Other Revenue'          : list(other_sims_table['Other Revenue']),
        'Total Revenue'          : list(other_sims_table['Total Revenue']),
        'Expense'                : list(other_sims_table['Expense']),
        'Margin'                 : list(other_sims_table['Margin']),

}