以虚线绘制的样式化数据帧

2024-06-16 13:24:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我有熊猫数据框,正在使用“df_styled=df.style.apply(…)”方法方便地设置数据框中的值的样式(在Jupyter笔记本中)。 但是,在使用Dash Plotly dashboard时,我无法显示此样式输出

有人能提出什么建议吗

我尝试了df_styled.render()方法,但这在Dash dashboard上显示了我的样式化数据框的字符串表示,而不是HTML输出(正如我在Jupyter笔记本中看到的)


Tags: 数据方法字符串dfstyle笔记本jupyter样式
1条回答
网友
1楼 · 发布于 2024-06-16 13:24:02

与直接使用Pandas的样式化组件不同,我建议使用以下组件

  1. 将数据帧转换为绘图破折号数据表
  2. 根据plotly dash文档格式化dash数据表

下面是一个示例代码



mytable = dash_table.DataTable(
    id='table1',
    columns=[{"name": i, "id": i,'type':'numeric', 'format': {'specifier': ',.0f'}}  if i not in ['phone_number'] else {"name": i, "id": i} for i in df.columns],
    data=df.to_dict('records'),
    style_as_list_view=True,
    style_cell={'padding': '2px'},
    page_action='none',
    style_table={'height': '600px', 'overflowY': 'auto'},
    style_cell_conditional=[
    {
        'if': {'column_id': 'phone_number'},
        'textAlign': 'center'
    },
],
    style_data_conditional=[
    {
        'if': {'row_index': 0},
        'backgroundColor': '#FFF2CC',
    },
],
    style_header={
        'backgroundColor': 'white',
        'fontWeight': 'bold',
        'textAlign': 'center'
    },
)

enter image description here

相关问题 更多 >