1

I am building an streamlit app. I put check box which indicates to each Data Frame. Then if the check box is clicked, concat all the relevant Data Frames.

for instance if option 1 and 2 are clicked, I want to concat only dataframe 1 and 2.

I wrote some piece of code which I can not get the final result, can anyone help to modify the code?


option_1 = st.sidebar.checkbox('dataframe1 ')
option_2 = st.sidebar.checkbox('dataframe2 ')
option_3 = st.sidebar.checkbox('dataframe3 ')
option_4 = st.sidebar.checkbox('dataframe4 ')

dic = {"option_1":"dataframe_1 ", "option_2" :"dataframe_2 ",
         "option_3":"dataframe_3 ", "option_4": "dataframe_4 ",
        }



df = None
for key, val in dic.items():
    if option_1 or option_2 or option_3 or option_4:
df = pd.concat([dataframe_1,dataframe_2,dataframe_3,dataframe_4])
else:
    None

My another try:


df = None
list2 = []
for key, val in dic.items():
    st.write(key)
    if option_1 or option_2 or option_3 or option_4 :
        list2.append(val)
        st.write(list2)

        for i in list2:
            df = pd.concat(i)
    else:
        None
user14269252
  • 412
  • 4
  • 15

2 Answers2

1

Usually, instead of separate variables _1, _2, ... _n, you want a list or tuple. These data structures are iterables, which means you can easily loop over them and do operations on them in aggregate. See How do I create variable variables? for further motivation.

Here's an example of how you might concat an iterable of dfs based on checkboxes:

import pandas as pd  # 1.5.1
import streamlit as st  # 1.18.1

dfs = st.session_state.dfs = st.session_state.get("dfs", (
    pd.DataFrame({"df 0": [0, 1]}),
    pd.DataFrame({"df 1": [2, 3]}),
    pd.DataFrame({"df 2": [4, 5]}),
    pd.DataFrame({"df 3": [6, 7]}),
))
checkboxes = [st.checkbox(f"df {i}") for i in range(len(dfs))]
to_concat = [df for df, checked in zip(dfs, checkboxes) if checked]
st.write(pd.concat(to_concat) if to_concat else pd.DataFrame())

I've cached the dfs in state as somewhat of a premature optimization, but this is optional.

ggorlen
  • 44,755
  • 7
  • 76
  • 106
1
# OP Entered
option_1 = st.sidebar.checkbox('dataframe1 ')
option_2 = st.sidebar.checkbox('dataframe2 ')
option_3 = st.sidebar.checkbox('dataframe3 ')
option_4 = st.sidebar.checkbox('dataframe4 ')

# Once you click the button, collect the checkboxes
if st.sidebar.button("Build"):
    dfs = []
    if option_1:
        dfs.append(dataframe_1)
    if option_2:
        dfs.append(dataframe_2)
    if option_3:
        dfs.append(dataframe_3)
    if option_4:
        dfs.append(dataframe_4)

# Logic checking
if len(dfs) > 1:
    df = pd.concat(dfs)        
elif len(dfs) == 1:
    df = dfs[0]
else:
    print("You did not select any dataframes")
artemis
  • 6,857
  • 11
  • 46
  • 99
  • Thank you, how do I pre select one of the check box here? – user14269252 Feb 24 '23 at 12:13
  • 2
    In the instantiation of the checkbox, set `value=True`. So let's say you wanted the first to be preselected, you would change: `option_1 = st.sidebar.checkbox('dataframe1 ')` to `option_1 = st.sidebar.checkbox('dataframe1 ', value=True)` – artemis Feb 24 '23 at 15:50