Declaring columns in groups on Dash Datatable with for loop

Hello, I’m new to Dash and I’ve been trying to do something for a while.

Let me explain the title better, suppose I have a dataframe with say 30 columns, but columns 1 to 10 are text, 11 to 25 are numbers and 26 to 30 are text again, I want to declare the columns with their respective types because I want to set a specific format to the numbers, but of course I don’t want to declare all columns as numeric because that would probably break any text that contains diacritics and special characters. Note that this could apply to any situation where one would want to declare columns in groups to apply certain settings in an automated way form using loops, instead of writing each one.

So, the closest I’ve found here to solve this problem is this post, but I’d have to declare each column separately, which doesn’t scale very well, I want to do this using a for loop. I think my question is somewhat specific and/or I couldn’t phrase it well to find a solution on google or here.

I also read the conditional formatting page, but I didn’t find any option using columns index, only row index or columns ids.

Anyway, here’s what I’ve tried, with data generated from gapmider:

from dash import Dash, html, dcc, dash_table
from dash.dash_table.Format import Format, Scheme
import as px
import pandas as pd

# App
app = Dash(__name__)

# Data
df =

# Number formatting
numformat = Format()
numformat = numformat.decimal_delimiter(',').precision(3).scheme('r')

# Slicing the data
dfnum = df.iloc[:, [2,3,4,5,7]]
dftext = df.iloc[:, [0,1,6]]

# Declaring the columns
dictnum = [{'name': i, 'id': i, 'type': 'numeric', 'format': numformat} for i in dfnum]
dicttext = [{'name': i, 'id': i} for i in dftext]

# Layout
app.layout = html.Div([
        columns=[dictnum , dicttext],
        style_cell={'textAlign': 'center'},

# Run the app
if __name__ == '__main__':
    app.run_server(debug=True, port=8051)

Running this code returns no error in Pycharm but the following error in the web app (I couldn’t paste the screenshot for some reason):

Invalid argument columns[0] passed into DataTable.
Expected object.
Was supplied type array.

The app works fine if I replace

columns = [dictnum, dicttext],

with either one of the list of dictionaries, for example:

columns = dictnum,

So I suppose the problem happens because columns must be a list, but it can’t be a list of lists, which is why I printed both dictnum and dicttext, to check that both where already enclosed by brackets so they were a list, for the code to work it seems I would need to create a bunch of dictionaries outside of a list and then put them into the columns variable, but I don’t know how to do that or if it’s even possible, because I need to use brackets while creating the dictnum and dicttext variables.

Hi @Galliard


columns= dictnum + dicttext,

I had a feeling it would be something that simple, but really had no idea how to solve the problem by myself, thanks.

1 Like