首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何替换Bokeh DataTable中的索引列?

如何替换Bokeh DataTable中的索引列?
EN

Stack Overflow用户
提问于 2021-08-18 07:54:27
回答 1查看 181关注 0票数 1

我在Bokeh中创建了一个DataTable,但它没有显示索引列:

我希望"Index“列在左边:

这是我的代码:

代码语言:javascript
复制
evolution_data = treatcriteria_daily_data_table.groupby(['startdate_dayweek','startdate_weekyear'],as_index = False).sum().pivot('startdate_dayweek','startdate_weekyear').fillna(0)

evolution_data = evolution_data.droplevel(0,1)

evolution_data.loc['Total'] = evolution_data.sum()
evolution_data['Index'] = ['Lundi', 'Mardi', 'Mercredi', 'Jeudi', 'Vendredi', 'Samedi', 'Dimanche', 'Total']
evolution_data.set_index('Index', inplace=True, drop=True)

# remove the last week if there is not all the data 
evolution_data = evolution_data.loc[:, ~(evolution_data == 0.).any()]

evolution_two_last_weeks = []

nb_cols = len(evolution_data.columns)
diff_cpu_2_last_weeks = evolution_data.iat[7, nb_cols - 1] - evolution_data.iat[7, nb_cols - 2]

for i in range(0,7):
    daily_evolution = (data_2_weeks_before_the_last.iat[1,i] - data_2_weeks_before_the_last.iat[0,i]) / diff_cpu_2_last_weeks
    evolution_two_last_weeks.append(daily_evolution)

variation_of_total = (data_2_weeks_before_the_last.iat[1,7] - data_2_weeks_before_the_last.iat[0,7]) / data_2_weeks_before_the_last.iat[0,7]

daily_variations_dict = {"Lundi": evolution_two_last_weeks[0],
                            "Mardi": evolution_two_last_weeks[1],
                            "Mercredi": evolution_two_last_weeks[2],
                            "Jeudi": evolution_two_last_weeks[3],
                            "Vendredi": evolution_two_last_weeks[4],
                            "Samedi": evolution_two_last_weeks[5],
                            "Dimanche": evolution_two_last_weeks[6],
                            "Total": variation_of_total}


# remove decimals in the table
cols = evolution_data.columns
evolution_data[cols] = evolution_data[cols].applymap(np.int64)

evolution_data['% évolution des 2 dernières semaines'] = evolution_data.index.map(mapper=(lambda x: daily_variations_dict[x]))

evolution_data['% évolution des 2 dernières semaines'] = pd.Series(["{0:.2f}%".format(val*100) for val in evolution_data['% évolution des 2 dernières semaines']], index = evolution_data.index)


print(evolution_data)

Columns = [TableColumn(field=Ci, title=Ci) for Ci in evolution_data.columns] # bokeh columns
data_table = DataTable(columns=Columns, source=ColumnDataSource(evolution_data)) # bokeh table

show(data_table)

如何显示索引列(带有日期名称)而不显示行号?谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-08-19 17:53:13

正如在注释中所说的,Bokeh不支持更改第一列,第一列总是固定的,并指示从0开始的行号。然而,在幕后,Bokeh使用了允许此功能的SlickGrid,但此解决方案太复杂了,因为您需要首先在Bokeh模型中找到对SlickGrid对象的引用,然后在页面加载后立即替换JavaScript中的第一列。

更简单的方法是建议使用index_position = None隐藏第一列,这样您就可以执行data_table = DataTable(... , index_position = None),然后将DataFrame中的Index数据作为第一个数据添加到表的列中。它现在不在那里的原因是df.columns没有包括您需要的索引列。所以试试吧:

代码语言:javascript
复制
cols.insert(0, 'Index')
data_table = DataTable(columns=Columns, 
                       source=ColumnDataSource(evolution_data), 
                       index_position = None) # add this line
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68828493

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档