微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决使用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 举报,一经查实,本站将立刻删除。