使用streamlit和python读取上传的文件后如何更新数据帧

问题描述

我想使用 file_uploader 读取文件,但我有一个功能允许用户更新上传文件中的数据,而不是保存文件然后在桌面上重新打开保存的文件显示更新的数据。

我想要的是覆盖上传文件,以便在用户点击按钮替换

显示新的数据框

在上图中,我想在用户cat6 更改为 后用 更新后的文件 替换文件 book2.xlsx猫1

代码

import pandas as pd
import streamlit as st 

def main():
    
   
    Activities = ["EDA","Plot","About"]
    choice = st.sidebar.selectBox("Select Activity",Activities)
    
    radio = st.sidebar.radio(label="",options=["Single File","Multiple Files"])
    
    df = pd.DataFrame()
    pt1=re.compile(".csv$")
    pt2=re.compile(".xlsx$")

    if radio == "Multiple Files":
        data = st.sidebar.file_uploader('Multiple Excel files',type=["csv","xlsx","xls"],accept_multiple_files=True)
    elif radio=="Single File":    
        data = st.sidebar.file_uploader("Upload Dataset","xls"])

    if data is not None:
        #EDA Page
        if choice =="EDA":
            st.subheader("Exploratiry Data Analysis")
    
            if radio=="Single File":
                if data.type =="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":
                    df=pd.read_excel(data)



#show replace     

            if st.checkBox("replace"):

                mydf = st.dataframe(df)

                columns = st.selectBox("Select  column",df.columns)

                old_values = st.multiselect("Current Values",list(df[columns].unique()),list(df[columns].unique()))

                with st.form(key='my_form'):

                    col1,col2 = st.beta_columns(2)

                    st_input = st.number_input if is_numeric_dtype(df[columns]) else st.text_input

                    with col1:

                        old_val = st_input("old value")

                    with col2:

                        new_val = st_input("new value")

                    if st.form_submit_button("Replace"):

                        df[columns]=df[columns].replace(old_val,new_val)

                        st.success("{} replace with {} successfully ".format(old_val,new_val))


                        excel = df.to_excel(r"F:\book2.xlsx",index = False,header=True,encoding="utf-8")

                        df =pd.read_excel(r"F:\book2.xlsx")       

                        mydf.add_rows(df)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)