I have examined my code and confirmed that it operates without errors. Nevertheless, as demonstrated in the accompanying image, the dataframe presents all accessible columns from which users may make selections via a list interface. My goal is to facilitate user selection of particular columns and subsequently transfer only those chosen columns to a new st.dataframe when a button is activated. Kindly furnish the requisite code to enable the transfer of exclusively selected columns from dataframe1 to dataframe2.
Welcome to the Streamlit community, and thanks for your clear question! To let users select specific columns from your dataframe and display only those columns in a new st.dataframe when a button is clicked, use st.multiselect for column selection and filter the dataframe based on the user’s choices. Here’s a concise example:
import streamlit as st
import pandas as pd
df = pd.read_excel('zahid.xlsx')
st.write(df.head())
columns = st.multiselect("Select columns to display", df.columns.tolist(), default=df.columns.tolist())
if st.button("Click Me"):
st.dataframe(df[columns])
This code displays a multiselect widget for column selection. When the button is pressed, only the selected columns are shown in the new dataframe. For more details, see the Streamlit multiselect documentation and dataframe display guide.
Using a multiselect widget for the column names should make this fairly simple. Once the user clicks the button, you can use the selected column list to create a second dataframe containing only those columns.