Python 仪表板数据表中的字段正在联接
我有以下数据框: 当我将其转换为dict,然后使用它填充一个dash数据表时,字段驱动程序如下所示被连接 我希望Python 仪表板数据表中的字段正在联接,python,pandas,datatable,plotly-dash,Python,Pandas,Datatable,Plotly Dash,我有以下数据框: 当我将其转换为dict,然后使用它填充一个dash数据表时,字段驱动程序如下所示被连接 我希望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_opti
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
数组中的所有元素,则每个元素将是一个由两个元素组成的数组。由于外部数组中的每个元素看起来像这样,我们可以简单地用逗号将子数组中的第一个和第二个元素连接起来。是的,熊猫
读取此输入的方式很有趣。“你说的很好。”拉贾特很乐意帮忙。我想让你知道,除了接受答案之外,你还可以选择你认为有用的答案(如果你至少有15个声誉)。我不是在这里寻找选票,但是注意到你没有把其他用户发布的答案投给你的问题,所以我想指出这一点。是的,很抱歉。很久以前我就试着向上投票,但由于声誉问题,我没能做到;我不知道它现在可以。很高兴你指出了这一点:)