【问题标题】:How to get rid of dataframe index name with plotly dash如何使用绘图破折号摆脱数据框索引名称
【发布时间】:2021-12-02 02:51:53
【问题描述】:

我有一个如下所示的示例数据框:

                894385426F4B81  8932F0978F4B80  89434C3243F4B81  899AD7554F4B80
metric          82.557         227.063           9.193          91.205

但是当我生成一个带有破折号的表格时:

def generate_table(dataframe, max_rows=10):
    return html.Table([
        html.Thead(
            html.Tr( [html.Th(col) for col in dataframe.columns])
            
        ),
        html.Tbody([
            html.Tr([

                html.Td(dataframe.iloc[i][col]) for col in dataframe.columns
            ]) for i in range(min(len(dataframe), max_rows))
            
        ])
    ])


app = dash.Dash(__name__)

app.layout = html.Div([
    html.H4(children='table name'),
    generate_table(output)
])

我看到这个索引名称的表:

index          894385426F4B81  8932F0978F4B80  89434C3243F4B81  899AD7554F4B80
metric         82.557           227.06299999999996   9.193     91.205

我拔头发是为了摆脱index这个名字

对于数据框本身,这是可行的,这意味着我在终端中看到的数据框没有index

output = output.rename_axis(None, axis=0)

我也试过了:

output = output.rename_axis('', axis=1)
output = output.index.set_names([''])
output.columns.name = None
output.index.name = None

没有任何帮助!

顺便说一句,我需要使用 output = output.reset_index(),否则 plotly dash 不会打印“metric”

【问题讨论】:

    标签: python pandas dataframe plotly-dash


    【解决方案1】:

    reset_index 自动添加index 作为列名。如果不需要显示,将index 列重命名为reset_index 之后的空字符串:

    output.reset_index().rename(columns={'index': ''})
    
               894385426F4B81  8932F0978F4B80  89434C3243F4B81  899AD7554F4B80
    0  metric          82.557         227.063            9.193          91.205
    

    【讨论】:

      猜你喜欢
      • 2012-12-05
      • 1970-01-01
      • 2020-04-22
      • 1970-01-01
      • 2019-07-02
      • 2011-11-05
      • 1970-01-01
      • 2022-09-25
      • 2017-05-25
      相关资源
      最近更新 更多