如何在pandas中删除选定列?

2024-09-22 10:30:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个由3列组成的数据框架

我希望允许用户选择要删除的列

当我尝试运行程序时,它会显示以下错误:

DuplicateWidgetID: There are multiple identical st.multiselect widgets with the same generated key.

When a widget is created, it's assigned an internal key based on its structure. Multiple widgets with an identical structure will result in the same internal key, which causes this error.

    To fix this error, please pass a unique key argument to st.multiselect.
    
    Traceback:
    File "F:\AIenv\streamlit\app.py", line 366, in <module>
        sidebars[y]=st.sidebar.multiselect('Filter '+y, df[y].unique())

代码:

import pandas as pd
import streamlit as st 

df =pd.DataFrame({
            "source_number":                        [ 
             [11199,11328,11287,32345,12342,1232,13456,123244,13456],
             "location":          
             ["loc2","loc1-loc3","loc3","loc1","loc2-loc1","loc2","loc3-loc2","loc2","loc1"],
              "category": 
             ["cat1","cat2","cat1","cat3","cat3","cat3","cat2","cat3","cat2"],
             })    


def remove_columns(dataset,cols):
    for col in cols:
        del dataset[col]
    return dataset

sidebars = {}
        
for y in columns:
    ucolumns=list(df[y].unique())
        
    sort_box = st.sidebar.checkbox("remove white-space in  {} column".format(y))
    splitting_box = st.sidebar.checkbox("split {}".format(y))
    if sort_box == True and splitting_box==True:
       df[y] = df[y].apply(processText)
       df=splitting(df,y)
    elif splitting_box ==True:
       df=splitting(df,y)
    elif sort_box ==True:
       df[y] = df[y].apply(processText)
    else:
       sidebars[y]=st.sidebar.multiselect('Filter '+y, df[y].unique())
    sidebars[y]=st.sidebar.multiselect('Filter '+y, df[y].unique())
    

Tags: keyinboxtruedffilteruniquest