Python 破折号数据 Table 应该只显示选定的列

Python Dash Data Table should display only selected columns

我正在尝试使用数据表仅显示我的数据框中的 selected 列。我可以 select 我想要多少行。寻找类似行的选项,我想 select 在执行代码时单独显示某些列。

我的数据框有将近 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',...]

已修复

您也可以按索引使用:

df = pd.read_csv('E:\pylab\dshlab\infratickets.csv', low_memory = False ) # load in the dataframe, then ressign with just the columns you want
df = df.iloc[:,1:3] # Remember that Python does not slice inclusive of the ending index.

将给出数据框的所有行和列 1 到 2。

您可以更改

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

如下:

先定义TABLE_SELECTED_COLUMNS = ['col1','col2'. ...]

columns=[{"name": i, "id": i} for i in TABLE_SELECTED_COLUMNS],