Problem using selectbox function

Hi, I am trying to use the selectbox function in my project with the following workflow :

  • get value from sqlite database
  • convert a column into a list
  • get a value with a selectbox function
  • use this value into another “select statement”

The code i wrote works well for the first element of the list, but as soon as I change the value with the dropdown list, I get an error really not explicit. Here is my code :

import streamlit as st
import pandas as pd
import sqlite3

conn = sqlite3.connect('myDB.db')  # Connection / Creation of the DataBase
c = conn.cursor()
conn.commit()

df1 = pd.read_sql_query('select col1, col2 '
                            'from "myTable1" , conn)

list = df1['col1'].to_list()
selection = st.selectbox('Choose value :', list)
get_id = df1['col2'].loc[df1['col1'] == selection][0]

df2 = pd.read_sql_query('SELECT * FROM myTable2 WHERE ID = ' + str(get_id ), conn)

st.dataframe(data=df2)

And the error I get is simply

KeyError: 0
Traceback:

It says the error is at this level : get_id = df1[‘col2’].loc[df1[‘col1’] == selection][0] but to my mind this is good because it runs well with the first element of the list and as I said : I only have the error when I change the value of the selectbox.

Thank you for your help!

This isn’t a Streamlit problem, your python code isn’t OK. You need to provide sample data because it’s not clear what you’re trying to do.

get_id = df1['col2'].loc[df1['col1'] == selection][0]

Here with [0] you’re subsetting the first row of df1, that’s why you get KeyError, get_id can’t ever return anything other than the first entry.
Also not a good idea to be using .loc on a column, can you write down what your filtering criteria should be?

In fact what I get from this line :

get_id = df1['col2'].loc[df1['col1'] == selection][0]

is a serie with one element. So this is why I put the [0] : to get the first element of the serie. And when I run this code from my IDE it works well.

Here is a sample I succeded to reproduce the error :

import streamlit as st
import pandas as pd

d1 = {'Id': [0, 1, 2], 'Colors': ['Red', 'Blue', 'Green']}
df1 = pd.DataFrame(data=d1)
d2 = {'Id': [0, 0, 1, 1, 2, 2], 'Things': ['Blood', 'Tomatoes', 'Sky', 'Ocean', 'Grass', 'Basil']}
df2 = pd.DataFrame(data=d2)

l = df1['Colors']
selection = st.selectbox('Choose your color :', l)
id_selected = df1['Id'].loc[df1['Colors'] == selection][0]

df = df2.loc[df2['Id'] == id_selected]

st.dataframe(data=df)

Thank you for your time!

1 Like

I think the problem is how you are using the pandas Series. Instead of:

id_selected = df1['Id'].loc[df1['Colors'] == selection][0]

Try:

id_selected = df1['Id'].loc[df1['Colors'] == selection].values[0]

Or better still:

id_selected = df1[df1['Colors'] == selection].Id.values[0]

Let me explain


d1 = {'Id': [0, 1, 2], 'Colors': ['Red', 'Blue', 'Green']}
df1 = pd.DataFrame(data=d1)
selection = 'Green'
id_selected = df1['Id'].loc[df1['Colors'] == selection]
print(type(id_selected), id_selected)

>>> <class ‘pandas.core.series.Series’> 2 2
>>> Name: Id, dtype: int64

So when you do this you get a KeyError:

id_selected[0]

Instead you need to access the ‘value’ using:

id_selected.values[0] # or id_selected.item()
1 Like

Thank you very much for this quick answer. What was weird is that I do had the good value when I ran my code through the debug mode.
But it’s clearn now! Thank you again

Really enjoying streamlit!

1 Like