gpt4 book ai didi

python-3.x - Python Dash 数据表应仅显示选定的列

转载 作者:行者123 更新时间:2023-12-02 02:58:28 29 4
gpt4 key购买 nike

我尝试使用 datatable 仅显示数据框中选定的列。我可以选择我想要的行数。寻找类似的选项,例如行,我想选择在执行代码时单独显示某些列。

我的数据框有接近 25 列。我不希望显示所有内容,因此寻找此解决方案

这是我的代码:

import dash
import dash_core_components as dcc
import dash_bootstrap_components as dbc
import dash_html_components as html
import dash_table as dt
from dash.dependencies import Input, Output
import plotly.graph_objs as go
import plotly.express as px
import pandas as pd

df = pd.read_csv('E:\pylab\dshlab\infratickets.csv', low_memory = False )

app = dash.Dash(__name__)
#style={'visibility': 'hidden'}

dpdown = []
for i in df['ASSIGNED_GROUP'].unique() :
str(dpdown.append({'label':i,'value':(i)}))

app.layout = html.Div([
html.P([
html.Label("Choose a feature"),
html.Div(dcc.Dropdown(id='dropdown', options=dpdown),
style = {'width': '100px',
'fontSize' : '10px',
'padding-left' : '100px',
'display': 'inline-block'})]),


#style={'visibility': 'hidden'},
html.Div(id='table-container', className='tableDiv'),
dcc.Graph(id = 'plot',style={'height' : '25%', 'width' : '25%'})

])
#dcc.Dropdown(id='dropdown', style={'height': '30px', 'width': '100px'}, options=dpdown),
#dcc.Graph(id='graph'),
#html.Div(html.H3('country graph'),id='table-container1',className='tableDiv1')





@app.callback(
dash.dependencies.Output('table-container','children'),
[dash.dependencies.Input('dropdown', 'value')])

def display_table(dpdown):
df_temp = df[df['ASSIGNED_GROUP']==dpdown]
return html.Div([
dt.DataTable(
id='main-table',
columns=[{'name': i, 'id': i} for i in df_temp.columns],
data=df_temp[0:5].to_dict('rows'),
style_table={
'maxHeight': '20%',
#'overflowY': 'scroll',
'width': '30%',
'minWidth': '10%',
},
style_header={'backgroundColor': 'rgb(30, 30, 30)'},
style_cell={'backgroundColor': 'rgb(50, 50, 50)','color': 'white','height': 'auto','width': 'auto'},#minWidth': '0px', 'maxWidth': '180px', 'whiteSpace': 'normal'},
#style_cell={'minWidth': '120px', 'width': '150px', 'maxWidth': '180px'},
style_data={'whiteSpace': 'auto','height': 'auto','width': 'auto'}

)

])



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

最佳答案

能够找出解决方案

修改了代码

columns=[{'name': i, 'id': i} for i in df_temp.columns]

columns=[{'name': i, 'id': i} for i in df.loc[:,['Colname1','Colname2',...]

已修复

关于python-3.x - Python Dash 数据表应仅显示选定的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60560101/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com