0

I have a python code that connect with sql server and retrieve the required data based on select query and display the result as a table with columns and rows.

The problem is that the retrieved data is displayed without columns name as shown in the picture below.

enter image description here

code:

import pandas as pd
import streamlit as st

st_input_update = st.number_input if is_numeric_dtype(all_columns) else st.text_input
search_term=st_input_update("Enter Search Term")
                        
sql='select * from testDB.dbo.t1 where last LIKE  ? '
param=f'%{search_term}%'
rows = cursor.execute(sql, param).fetchall() 
st.dataframe(rows)
                                 

2 Answers 2

1

To use st.dataframe You need to create a pandas Dataframe.
What you get after your query is not a DataFrame, it is a list of tuples.

Try this

df = pd.DataFrame(rows, columns = ["ID", "first_name", "last_name"])  

st.dataframe(df)
Sign up to request clarification or add additional context in comments.

3 Comments

i tried your answer it display the below error: ValueError: Shape of passed values is (6, 1), indices imply (6, 3)
i fix it by adding this function to the dataframe .from_records()
Ah ok. Your code implies that it would be (6,3). You should post your .from_records() as it's own answer to this post.
1

I fixed it by adding this function to the dataframe .from_records() so the line of code becomes:

df = pd.DataFrame.from_records(rows, columns =columns)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.