How to get st.dataframe columns statue is hide or show

dear boss
Please refer to the information below regarding the st.dataframe images. The first image displays all columns, the second image conceals certain columns, and the third image indicates the visibility status of the columns, which can be toggled using a checkbox. I require a list of the status of these columns, including their names and whether they are hidden or visible. Kindly advise me on how to obtain the visibility status of the st.dataframe columns.

like as

colid status
col1 1
col2 1
col3 1
col4 0
col5 0
col6 1
col7 0

all columns


Hi @badar

Please wait for a response from the forum.

Alternatively, a sample workaround is as below:

import streamlit as st
import pandas as pd

tdf = pd.DataFrame({'A': [1000, 2000, 3000], 'B': [4000, 5000, 6000], 'C': [18, 83, 64]})

col_lst = tdf.columns.to_list()
col_lst = {col_lst[i]: True for i in range(len(col_lst))}

sc1, sc2 = st.columns((2,8), vertical_alignment='bottom')
sc1.write("Select Cols:")
for k, v in col_lst.items(): col_lst[k] = sc1.checkbox(label=k, value=v)
sc2.dataframe(tdf[[key for key, value in col_lst.items() if value]], use_container_width=False)

tmp

Cheers

Ok boss ok
i am your new student
sorry

Sir, your code runs well; however, I would like to know how to transfer it to a new DataFrame and convert it into a list. Please guide me, as I have attempted some code but encountered errors. Kindly provide the code for transferring this selected column to a new DataFrame.

both code give me error
dddd = sc2.dataframe(tdf[[key for key, value in col_lst.items() if value]], use_container_width=False).tolist()

newdf = pd.DataFrame(sc2.dataframe)

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