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

问题描述 投票:1回答:1

我已经根据.describe()的输出创建了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是我编写的函数,它返回montecarlo模拟的不同迭代的数据帧输出。除了这一列之外,一切似乎都在起作用。

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

以下是输出:enter image description here

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

python python-3.x pandas bokeh
1个回答
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'的新列,然后在main函数中没有被引用。正确的代码是:

    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']),

}
© www.soinside.com 2019 - 2024. All rights reserved.