Home > Blockchain >  How to get rid of dataframe index name with plotly dash
How to get rid of dataframe index name with plotly dash

Time:10-14

I have a sample dataframe which looks like this:

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

But when I generate a table with plotly dash:

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

I see this table with this index name:

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

I pulling my hair out to get rid of the name index

for the dataframe itself this works, means I see the dataframe in my terminal without index:

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

I also tried:

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

Nothing helps!

Btw, I need to use output = output.reset_index(), otherwise plotly dash doesn't print "metric"

CodePudding user response:

reset_index automatically adds index as column name. Rename index column as empty string after reset_index if you don't need to display it:

output.reset_index().rename(columns={'index': ''})

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