I need to get the name of the columns of the dataframe and apply in a dropdown

I need to get the name of the columns of the dataframe and apply in a Dropdown id=‘check_opcao’ to be able to select. and then in the callback function I need to filter the dataframe to select only the columns where it corresponds to the value I selected in the Dropdown

I tried a way where I created a new column in df called (Options) colunas = ['Player','Rnd','ACS','K:D','KAST','ADR','KPR','APR','FKPR','FDPR','HS%','CL%','CL','KMax','K','D','A','FK','FD','Team'] df['Colunas'] = pd.Series(colunas)
and added the values with the names of each column but when I try to filter tabela_filtrada = df.loc[df['Colunas'] == check_opcao,:] it returns only the rows that correspond to my selection in the dropdown…

colunas = ['Player','Rnd','ACS','K:D','KAST','ADR','KPR','APR','FKPR','FDPR','HS%','CL%','CL','KMax','K','D','A','FK','FD','Team']
df['Colunas'] = pd.Series(colunas)

fig = px.bar(df, x='Player', y='ACS',  title='Top 10 jogadoras com o maior ACS no Game Changers Brasil:', color='Player', barmode = 'stack', 
            labels={
                    'Player': 'Nome e Time',
                    'ACS': 'ACS'}, template='plotly_dark', text='ACS')

opcoes = list(df['Colunas'].unique())

app.layout = html.Div(children=[

        html.H5('Opcões'),
        dcc.Dropdown(opcoes, value = 'ACS' , id='check_opcao'),

        dcc.Graph(figure=fig, id='grafico_gc_players')
        
        
        ])
@app.callback(
        Output('grafico_gc_players', 'figure'),
                [
                Input('check_opcao', 'value'),
                
                ])

def renderizar_graficos(check_opcao):


    if check_opcao == 'ACS':
        fig = px.bar(df, x='Player', y='ACS',  color='Player', barmode = 'stack', 
            labels={
                    'Player': 'Nome e Time'}, template='plotly_dark')
    else:
        tabela_filtrada = df.loc[df['Colunas'] == check_opcao,:]
        fig = px.bar(tabela_filtrada, x='Player', y= check_opcao, color='Player', barmode = 'stack', 
            labels={
                    'Player': 'Nome e Time'}, template='plotly_dark')

    return fig

@Luisanches,

You should be able to just pass the colunas directly into the options argument on the dcc.Dropdown check_opcao.

1 Like

I passed these arguments, it worked, it keeps showing me the options in the Dropdown but now the error is occurring in the filter

I don’t know how I can filter the DF I’m trying this:

tabela_filtrada = df.loc[df['Player','Rnd','ACS','K:D','KAST','ADR','KPR','APR','FKPR','FDPR','HS%','CL%','CL','KMax','K','D','A','FK','FD','Team'] == check_opcao,:]

but it gives this error:

KeyError: ('Player', 'Rnd', 'ACS', 'K:D', 'KAST', 'ADR', 'KPR', 'APR', 'FKPR', 'FDPR', 'HS%', 'CL%', 'CL', 'KMax', 'K', 'D', 'A', 'FK', 'FD', 'Team')

@Luisanches,

What data is in the column that you are trying to filter by?

Or are you just trying to display the selected column on the chart?

i’m trying to display column values ​​in chart

@Luisanches,

See if this is what you are after.

colunas = ['Player','Rnd','ACS','K:D','KAST','ADR','KPR','APR','FKPR','FDPR','HS%','CL%','CL','KMax','K','D','A','FK','FD','Team']
df['Colunas'] = pd.Series(colunas)

fig = px.bar(df, x='Player', y='ACS',  title='Top 10 jogadoras com o maior ACS no Game Changers Brasil:', color='Player', barmode = 'stack', 
            labels={
                    'Player': 'Nome e Time',
                    'ACS': 'ACS'}, template='plotly_dark', text='ACS')

opcoes = list(df['Colunas'].unique())

app.layout = html.Div(children=[

        html.H5('Opcões'),
        dcc.Dropdown(colunas, value = 'ACS' , id='check_opcao'),

        dcc.Graph(figure=fig, id='grafico_gc_players')
        
        
        ])
@app.callback(
        Output('grafico_gc_players', 'figure'),
                [
                Input('check_opcao', 'value'),
                
                ])

def renderizar_graficos(check_opcao):


    if check_opcao == 'ACS':
        fig = px.bar(df, x='Player', y='ACS',  color='Player', barmode = 'stack', 
            labels={
                    'Player': 'Nome e Time'}, template='plotly_dark')
    else:
        fig = px.bar(df, x='Player', y= check_opcao, color='Player', barmode = 'stack', 
            labels={
                    'Player': 'Nome e Time'}, template='plotly_dark')

    return fig

that’s exactly it! Did you just remove the DF filter?

1 Like

@Luisanches,

Pretty much, you just wanted to graph the different column, in fact you dont even need the else statement in that function. :wink:

Just this:

fig = px.bar(df, x='Player', y= check_opcao, color='Player', barmode = 'stack', 
            labels={
                    'Player': 'Nome e Time'}, template='plotly_dark')
1 Like

Damn, I didn’t even stop to think about it… I was following the example of a code I saw in a youtube video and was trying to apply it to my project… Thanks a lot for the help