Streamlit app – if the check box is clicked, concat all the relevant Data Frames that each check box is indicating

huangapple go评论81阅读模式
英文:

Streamlit app - if the check box is clicked, concat all the relevant Data Frames that each check box is indicating

问题

df = None
list2 = []
for key, val in dic.items():
    if locals()[key]:
        list2.append(locals()[val])

df = pd.concat(list2) if list2 else None
英文:

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

答案1

得分: 1

通常情况下,不使用单独的变量 _1_2、... _n,而是使用列表或元组。这些数据结构是可迭代的,这意味着您可以轻松地对它们进行循环操作并在聚合中执行操作。有关更多动机,请参阅如何创建可变变量?

以下是一个示例,演示了如何基于复选框合并可迭代的数据帧:

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())

我已将数据帧在状态中缓存,这有点提前优化,但这是可选的。

英文:

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.

答案2

得分: 1

OP 输入

option_1 = st.sidebar.checkbox('数据框1')
option_2 = st.sidebar.checkbox('数据框2')
option_3 = st.sidebar.checkbox('数据框3')
option_4 = st.sidebar.checkbox('数据框4')

单击按钮后,收集复选框

if st.sidebar.button("构建"):
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)

逻辑检查

if len(dfs) > 1:
df = pd.concat(dfs)
elif len(dfs) == 1:
df = dfs[0]
else:
print("您未选择任何数据框")

英文:
# 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")

huangapple
  • 本文由 发表于 2023年2月24日 01:44:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75548459.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定