Dash 数据表中的字段正在连接

Fields in Dash Datatable are joining

我有以下数据框:

当我将其转换为字典然后使用它来填充破折号数据表时,字段驱动程序如下所示连接

我希望 driver 字段中的元素以逗号分隔。我怎样才能做到这一点?我当前的代码如下:

import dash
import dash_table
import pandas as pd
import dash_bootstrap_components as dbc
import dash_html_components as html

## Display Settings
pd.set_option("max_columns",20)
pd.set_option("max_colwidth", 50)
pd.set_option("display.width", 2000)
pd.set_option("max_rows", 100)

import dash
import dash_html_components as html
import pandas as pd

brand_name = ['merc', 'bmw']

driver = [['driver1', 'driver2'], ['driver3', 'driver14']]

df = pd.DataFrame({'brand_name':brand_name, 'driver': driver})
print(df)

df_dict = df.to_dict('records')


app = dash.Dash(__name__)
app.layout = dbc.Card(
    dbc.CardBody(
        [
            dash_table.DataTable(
                id='homepage-table',
                data=df_dict,
                columns=[
                    {'name': 'brand_name', 'id':'brand_name'},
                    {'name': 'driver', 'id':'driver', },

            ],
style_cell={'textAlign': 'left', 'padding': '5px'},
                        style_data={
                            'whiteSpace': 'normal',
                            'height': 'auto',
                            'lineHeight': '18px',
                            'width': '22px',
                            'fontSize': '14px'
                        },
                        style_header={
                            'backgroundColor': 'rgb(230, 230, 230)',
                            'fontWeight': 'bold',
                            'lineHeight': '40px',
                            'fontSize' : '18px',
                            'padding-left': '5px'
                        },
                        style_data_conditional=[
                            {'if':
                                {
                                'row_index': 'odd'
                                },
                            'backgroundColor': 'rgb(248, 248, 248)'
                            },
                        ],
            )]))


if __name__ == '__main__':
    app.run_server(debug=True)

而不是这个:

driver = [['driver1', 'driver2'], ['driver3', 'driver14']]

df = pd.DataFrame({'brand_name':brand_name, 'driver': driver})

做这样的事情:

drivers = [["driver1", "driver2"], ["driver3", "driver14"]]
drivers_comma_separated = [x[0] + ", " + x[1] for x in drivers]

df = pd.DataFrame({"brand_name": brand_name, "driver": drivers_comma_separated})

所以我的想法是,如果您遍历 drivers 数组中的所有元素,每个元素将是一个包含两个元素的数组。由于外部数组中的每个元素看起来像这样 ["driver1", "driver2"] 我们可以简单地将子数组中的第一个和第二个元素彼此连接起来,中间用逗号连接。