【问题标题】:Python - Building a descriptive statistics table using dash_bootstrap_componentsPython - 使用 dash_bootstrap_components 构建描述性统计表
【发布时间】:2021-08-19 20:19:07
【问题描述】:

我正在使用 Dash 和 dash_bootstrap_components 制作仪表板。 我已经有一个包含 3 行图表的布局,在下拉选择(来自数据框)后会发生变化。 我想做但找不到答案的一件事是选择一个变量并获得一个直方图(我知道该怎么做)并获得一个包含一些统计值的表格(类似于 df.sescribe( ))。我在应用所有更改之前附加了模拟布局。

这样做的方法是什么? dbc.Table?

想了解如何从这里继续。

谢谢!

【问题讨论】:

    标签: python pandas plotly-dash dash-bootstrap-components


    【解决方案1】:

    我在 Dash 中制作了两种表格,使用dash_table 进行交互,使用dbc.Table 进行更具视觉吸引力的表格。下面是我创建的函数,它们允许我传递 pandas 数据框和一些用于格式化表格的附加参数。我在回调中使用这些函数来创建我想要的表并将其返回给html.Div 组件

    互动: dash_table

    import dash_table
    
    def make_interactive_dash_table(df, cell_width='150px', page_size=10, sort_cols=None, ascending=True):
        
        if sort_cols:
            data = df.sort_values(sort_cols, ascending=ascending).to_dict('records')
        else:
            data = df.to_dict('records')
    
        # tweak these if you need to
        # this helps format column widths when you enable sort_action
        long_column_names = [{"if": {"column_id": column}, "min-width": "300px"} for column in df.columns if len(column) >= 30]
        med_column_names = [{"if": {"column_id": column}, "min-width": "250px"} for column in df.columns if (len(column) > 15 and len(column)) < 30]
        small_column_names = [{"if": {"column_id": column}, "min-width": "150px"} for column in df.columns if len(column) <= 15]
    
        adjusted_columns = long_column_names + med_column_names + small_column_names
    
        table = dash_table.DataTable(
            columns=[{'name': i, 'id': i} for i in df.columns],
            data=data,
            filter_action='native',
            sort_action='native',
            style_table={'overflowX': 'auto'},
            style_cell_conditional=adjusted_columns,
            page_size=page_size
        )
    
        return table
    

    格式化表格: dbc.Table

    import dash_html_components as html
    import dash_bootstrap_components as dbc
    
    def make_formatted_dash_table(df, header=True, sort_cols=None, ascending=True, size='md'):
        
        if sort_cols:
            df.sort_values(sort_cols, ascending=ascending, inplace=True)
        
        rows, cols = df.shape
    
        if header:
            table_header = html.Thead(
                html.Tr([html.Td(i) for i in df.columns])
            )
        else:
            table_header = None
    
        table_body = html.Tbody(
            [
                html.Tr(
                    [
                        html.Td(df.iloc[row, col]) for col in range(cols)
                    ]
                ) for row in range(rows)
            ]
        )
    
        table = dbc.Table(
            [table_header, table_body],
            bordered=True,
            hover=True,
            striped=True,
            size=size
        )
    
        return table
    

    【讨论】:

    • 非常感谢查克!超级有帮助。会试试这个
    • @NsN 如果效果很好,请考虑将答案标记为已接受。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-29
    • 1970-01-01
    • 2019-05-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多