Add Header Names to st.dataframe generated from List of touples?

  1. Running App Locally
  2. Python 3.10 Streamlit 1.31

Hello,

I have some data I get from a SQLite3 Query, I want do display in a st.dataframe().
The query returns a list of tuples. The st.dataframe can handle this no problem, but I wanted to add column names to the dataframe.

I have already tried to add a column config like this:

column_config={
    1 : "App name",
    2 : "stars",
    3 : "url",
    4 : "views",
}

st.dataframe(df, column_config) 

But then I get an error:
“TypeError: ‘dict’ object cannot be interpreted as an integer”

Is there a way do add column names to a dataframe that is based on a list of tuples?

Thanks!
Thani

Convert that list of tuple to a pandas df.

aa = [('a', 1), ('b', 6)]
df = pd.DataFrame(aa, columns=['char', 'num'])
st.dataframe(df)

or without using pandas.

aa = [('a', 1), ('b', 6)]
dic = {'char': [elem[0] for elem in aa], 'num': [elem[1] for elem in aa]}
st.dataframe(dic)

Hello @AlThani,

Can you please try this:

import streamlit as st
import pandas as pd

data = [
    ('App A', 4.5, 'http://example.com/a', 1000),
    ('App B', 4.0, 'http://example.com/b', 1500),
    ('App C', 3.5, 'http://example.com/c', 2000),
]

column_names = ['App Name', 'Stars', 'URL', 'Views']

df = pd.DataFrame(data, columns=column_names)

st.dataframe(df)

Kind Regards,
Sahir

P.S. Lets connect on LinkedIn!

@sahirmaharaj Thank you, but I will go with the dictionary approach from @ferdy since this is the only place I would need pandas in the entire app. And since I can use something from the standard library I just go with this to keep the app as leightweight as possible.