'Dash DataTable Filter is not working for numeric columns
I created a table using Python Dash DataTable and added a filter to each column. The data table looks like this:
Text_Column Numeric_Column
abcde 12345
dfjke 34928
Each column has a filter but it seems like the filter works only when the column has text values. It's not working for the numeric column. How do I make it to work for both text and numeric columns?
dash_table.DataTable(
id="table",
columns=[
{"name": i, "id": i} for i in df.columns
],
data=df.to_dict('records'),
filter_action="native",
sort_action="native",
style_table={
"overflowX": "scroll",
},
row_selectable="multi",
style_cell={
"height": "auto",
"maxWidth": "200px",
"whiteSpace": "normal",
"font-family": "Arial",
'textAlign': 'center',
},
)
),
Solution 1:[1]
So I just had the same issue. I fixed it by correctly setting the column's type to "numeric".
Like this:
columns = []
for col in df.columns:
col_options = {"name": col, "id": col}
if col == "MyNumericColumn":
col_options["type"] = "numeric"
columns.append(col_options)
my_table = dash_table.DataTable(id="table", columns=columns)
Please note that some filter operations currently seem to have an issue related to the case sensitivity of the column. See this bug report: https://github.com/plotly/dash/issues/1793
Solution 2:[2]
So, a slight alternative if you wish to make any of your non string column to be filtered for any data you have.
columns = []
for col in df.columns:
col_options = {"name": col, "id": col}
for value in df[col]:
if not (isinstance(value, str)):
col_options["type"] = "numeric"
columns.append(col_options)
return columns
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | robert |
Solution 2 | Masua |