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

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

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

问题

  1. df = None
  2. list2 = []
  3. for key, val in dic.items():
  4. if locals()[key]:
  5. list2.append(locals()[val])
  6. 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?

  1. option_1 = st.sidebar.checkbox('dataframe1 ')
  2. option_2 = st.sidebar.checkbox('dataframe2 ')
  3. option_3 = st.sidebar.checkbox('dataframe3 ')
  4. option_4 = st.sidebar.checkbox('dataframe4 ')
  5. dic = {"option_1":"dataframe_1 ", "option_2" :"dataframe_2 ",
  6. "option_3":"dataframe_3 ", "option_4": "dataframe_4 ",
  7. }
  8. df = None
  9. for key, val in dic.items():
  10. if option_1 or option_2 or option_3 or option_4:
  11. df = pd.concat([dataframe_1,dataframe_2,dataframe_3,dataframe_4])
  12. else:
  13. None

My another try:

  1. df = None
  2. list2 = []
  3. for key, val in dic.items():
  4. st.write(key)
  5. if option_1 or option_2 or option_3 or option_4 :
  6. list2.append(val)
  7. st.write(list2)
  8. for i in list2:
  9. df = pd.concat(i)
  10. else:
  11. None

答案1

得分: 1

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

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

  1. import pandas as pd # 1.5.1
  2. import streamlit as st # 1.18.1
  3. dfs = st.session_state.dfs = st.session_state.get("dfs", (
  4. pd.DataFrame({"df 0": [0, 1]}),
  5. pd.DataFrame({"df 1": [2, 3]}),
  6. pd.DataFrame({"df 2": [4, 5]}),
  7. pd.DataFrame({"df 3": [6, 7]}),
  8. ))
  9. checkboxes = [st.checkbox(f"df {i}") for i in range(len(dfs))]
  10. to_concat = [df for df, checked in zip(dfs, checkboxes) if checked]
  11. 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:

  1. import pandas as pd # 1.5.1
  2. import streamlit as st # 1.18.1
  3. dfs = st.session_state.dfs = st.session_state.get("dfs", (
  4. pd.DataFrame({"df 0": [0, 1]}),
  5. pd.DataFrame({"df 1": [2, 3]}),
  6. pd.DataFrame({"df 2": [4, 5]}),
  7. pd.DataFrame({"df 3": [6, 7]}),
  8. ))
  9. checkboxes = [st.checkbox(f"df {i}") for i in range(len(dfs))]
  10. to_concat = [df for df, checked in zip(dfs, checkboxes) if checked]
  11. 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("您未选择任何数据框")

英文:
  1. # OP Entered
  2. option_1 = st.sidebar.checkbox('dataframe1 ')
  3. option_2 = st.sidebar.checkbox('dataframe2 ')
  4. option_3 = st.sidebar.checkbox('dataframe3 ')
  5. option_4 = st.sidebar.checkbox('dataframe4 ')
  6. # Once you click the button, collect the checkboxes
  7. if st.sidebar.button("Build"):
  8. dfs = []
  9. if option_1:
  10. dfs.append(dataframe_1)
  11. if option_2:
  12. dfs.append(dataframe_2)
  13. if option_3:
  14. dfs.append(dataframe_3)
  15. if option_4:
  16. dfs.append(dataframe_4)
  17. # Logic checking
  18. if len(dfs) > 1:
  19. df = pd.concat(dfs)
  20. elif len(dfs) == 1:
  21. df = dfs[0]
  22. else:
  23. 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:

确定