Dash DataTable - Columns Are Rearranged

Sure! The following should show you what i mean,

import dash
import dash_html_components as html
import dash_table_experiments as dt
import pandas as pd

app = dash.Dash()

app.scripts.config.serve_locally = True


df ={'y': [1,2,3],
                  '1': [4,5,6],
                  'x' : [7,8,9]} # automatically rearranged to be alphabetical (1, x, y)
df = pd.DataFrame(df)
#To set it back non-alphabetically,
df = df[['y', '1', 'x']]


#Produces a table with columns in the order 1,x,y
app.layout = html.Div([
    dt.DataTable(
        rows=df.to_dict('records'),
        columns=(df.columns),
        filters=True,
        resizable=True,
        sortColumn=True,
        editable=True,
        row_selectable=True,
        filterable=True,
        sortable=True,
        selected_row_indices=[],
        id='datatable-gapminder')])

#Produces a table with columns in the order y, 1, x (desired)
app.layout = html.Div([
    dt.DataTable(
        rows=df.to_dict('records'),
        columns=(df.columns),
        filters=True,
        resizable=True,
        sortColumn=True,
        editable=True,
        row_selectable=True,
        filterable=True,
        sortable=True,
        selected_row_indices=[],
        id='datatable-gapminder')])

app.run_server(debug=False, host='0.0.0.0')

The thing is, I want my table to be the result of a drop-down, similar to what you’ve got here, Dash DataTable - Updating Rows with Dropdowns?
and I dont know where I could add the columns command, so that they return in the order set in the dataframe.

1 Like