![](/img/trans.png)
[英]how to display all the records in the dataframe then based on filter replace it with the filtered dataframe using python and streamlit
[英]how to replace values on a dataframe using pandas and streamlit in python?
我有一個 python 腳本,它使用 pandas 讀取 dataframe 並使用流光顯示其內容。
我想要的是根據用戶輸入用新值替換當前值。
在用戶select 所需的列中,然后在文本字段中輸入當前值而不是在按下按鈕替換按鈕時在第二個文本字段中輸入新值時,舊值被新值替換並顯示新 dataframe。
問題是當它顯示 dataframe 時沒有任何改變
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","loc1","loc2","loc2","loc3","loc2","loc1"],
"category":
["cat1","cat2","cat1","cat3","cat3","cat3","cat2","cat3","cat2"],
})
columns = st.selectbox("Select column", df.columns)
old_values = st.multiselect("Current Values",list(df[columns].unique()),list(df[columns].unique()))
col1,col2 = st.beta_columns(2)
with col1:
old_val = st.text_input("old value")
with col2:
new_val = st.text_input("new value")
if st.button("Replace"):
df[columns]=df[columns].replace({old_val:new_val})
st.dataframe(df)
您的代碼中有一個小錯誤。
df[列]=df[列].replace({old_val:new_val})
當您查看示例中的 pandas 文檔時,會有這樣的代碼
s.replace({'a': None}) - 它用 None 值替換值 'a'
查看代碼的含義時,您試圖用另一個列表替換作為列表的值,但它不能那樣工作,因為您的列沒有列表作為元素,因此無法更改它像那樣。 當我在 Jupyter 中運行您的代碼時,我收到一個錯誤,即列表不可散列
--------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-5-41a02888936d> in <module>
30 oldVal = [11199,11328,11287,32345]
31
---> 32 df["source_number"] = df["source_number"].replace({oldVal:newVal})
33 df
TypeError: unhashable type: 'list'
這就是為什么它不會為您改變任何東西的原因。
如果要使用列表更改所有值,則必須這樣編寫:
df[column] = df[column].replace(old_values, new_values)
這段代碼工作得很好。
我希望我足夠清楚,它會為你工作。
您的代碼適用於文本列( location
和category
)。 當您嘗試將一個字符串替換為另一個字符串時,它不適用於數字source_number
列。
對於數字列,您需要使用number_input
而不是text_input
:
import pandas as pd
from pandas.api.types import is_numeric_dtype
import streamlit as st
df = pd.DataFrame({
"source_number":
[11199,11328,11287,32345,12342,1232,13456,123244,13456],
"location":
["loc2","loc1","loc3","loc1","loc2","loc2","loc3","loc2","loc1"],
"category":
["cat1","cat2","cat1","cat3","cat3","cat3","cat2","cat3","cat2"],
})
columns = st.selectbox("Select column", df.columns)
old_values = st.multiselect("Current Values",list(df[columns].unique()),list(df[columns].unique()))
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.button("Replace"):
df[columns]=df[columns].replace({old_val:new_val})
st.dataframe(df)
根據評論更新:您可以緩存df
以防止在每次小部件交互時重新初始化(您必須手動清除緩存才能重新開始):
@st.cache(allow_output_mutation=True)
def get_df():
return pd.DataFrame({
"source_number":
[11199,11328,11287,32345,12342,1232,13456,123244,13456],
"location":
["loc2","loc1","loc3","loc1","loc2","loc2","loc3","loc2","loc1"],
"category":
["cat1","cat2","cat1","cat3","cat3","cat3","cat2","cat3","cat2"],
})
df = get_df()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.