【问题标题】:How to show values based on the user selection in pandas dataframe如何根据熊猫数据框中的用户选择显示值
【发布时间】:2021-07-03 03:41:25
【问题描述】:

第二个多选的选项将根据预先存在的数据结构中的值进行更改。

import pandas as pd
import streamlit as st 
        
df = pd.DataFrame({"A":[4,6,6],
                  "B":[4,6,9],
                  "C":[1,2,3],
                 }) 
interactive = st.beta_container()    
col1, col2 = st.beta_columns(2)
            
with interactive: 
     all_columns_names= df.columns.tolist()
     with col1:
         option1 = st.multiselect("select column to plot",all_columns_names)
     with col2:
         option2 = st.multiselect("select required value/s to 
            plot",list(set(df.loc[(df.iloc[:,0].isin(option1))][df.columns[1]])))
                    

示例:

if   option1 = B
then option2 = [4,6,9]

if option1 = [A,C]
then option2 = [4,6,6,1,2,3]

col1 中语句的错误在哪里?如何获取与所选option1 相关的option2 的值?

【问题讨论】:

  • @KristianCanler 这更好吗?
  • 如果您收到错误代码,请同时包含该代码。除此之外,代码现在看起来很少/可重现,但为了清楚起见,问题可能需要更多解释。
  • @user5980666 说如果option1Aoption2的逻辑是什么?
  • @Ynjxsjmh if option1 is A option2 必须包含 [4,6,6]

标签: python pandas dataframe multi-select streamlit


【解决方案1】:

您可以通过迭代每列并将它们附加到容器中来获取列的值。

with interactive:
     with col2:
         vals = set()

         if not isinstance(option1, list):
             option1 = [option1]

         for col in option1:
             vals.update(df[col].values.tolist())

         option2 = st.multiselect("select required value/s to plot", list(vals))

【讨论】:

    猜你喜欢
    • 2015-08-15
    • 2018-08-05
    • 2021-11-30
    • 2015-10-23
    相关资源
    最近更新 更多