How to display multiple dataframes from a list of variable length in Streamlit app

Hello,

I have a collection of dataframes stored in a list of variable length [df1, df2]. How can I display both dataframes in my streamlit app?

Currently, I do:

for dataframe in [df1, df2]: 
      st.dataframe(dataframe)

this will only display one dataframe. How can I change my code in such a way that it shows all the dataframes from the list?

I am using Python 3.11.7 and Streamlit 1.30.0

Many thanks in advance!

1 Like

Hi Talita_Anthonio

You can modify your code to loop through the list of dataframes and display each one individually

import streamlit as st
import pandas as pd


data1 = {'Nombre': ['Juan', 'MarĂ­a', 'Pedro'],
         'Edad': [25, 30, 35],
         'Ciudad': ['Madrid', 'Barcelona', 'Sevilla']}
df1 = pd.DataFrame(data1)

data2 = {'Producto': ['A', 'B', 'C'],
         'Precio': [10, 20, 30],
         'Cantidad': [100, 200, 300]}
df2 = pd.DataFrame(data2)


dataframes = [df1, df2]


for i, dataframe in enumerate(dataframes):
    st.write(f"DataFrame {i+1}:")
    st.dataframe(dataframe)
2 Likes

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.