Hi,
i would like to create a dynamic dataframe ie I mean a dataframe that rows can be added from an other dataframe.
Suppose that i have a dataframe :
Name Fund
Asset Class
Weight
Fund1
Equity
5%
Fund2
Bond
10%
Fund3
Commodity
5%
From i would like to add a new row from an other dataframe and complete automatically (when i choose the fund) the asset class (and others informations that i have in this dataframe and which are not represented here.
Thanks
1 Like
ferdy
June 2, 2024, 11:53am
2
Have a look on our posting guide to make us understand your issues.
I have (a simplifier version of my code) 2 dataframe that are generated by this code:
import pandas as pd
import streamlit as st
# Créer les données
data = {
"Name Fund": ["Fund A", "Fund B", "Fund C"],
"Asset Class": ["Equity", "Fixed Income", "Commodity"],
"Weight": [0.4, 0.3, 0.3]
}
# Créer le DataFrame
df = pd.DataFrame(data)
data2 = {
"Name Fund": ["Fund D", "Fund E", "Fund F"],
"Asset Class": ["Equity", "Fixed Income", "Commodity"],
}
# Créer le DataFrame
df2 = pd.DataFrame(data2)
# Afficher le DataFrame
st.dataframe(df)
I would like to add data from df2 ie add a new row in my st.dataframe(df) instruction and when i choose Fund D it adds automatically the asset class.
Is it clearer?
Here’s one method that uses a dialogue to add a few row
import pandas as pd
import streamlit as st
if "data" not in st.session_state:
st.session_state["data"] = pd.DataFrame(
{
"Name Fund": ["Fund A", "Fund B", "Fund C"],
"Asset Class": ["Equity", "Fixed Income", "Commodity"],
"Weight": [0.4, 0.3, 0.3],
}
)
data2 = {
"Name Fund": ["Fund D", "Fund E", "Fund F"],
"Asset Class": ["Equity", "Fixed Income", "Commodity"],
}
# Créer le DataFrame
df2 = pd.DataFrame(data2)
# Afficher le DataFrame
st.dataframe(st.session_state["data"], hide_index=True)
@st.experimental_dialog("Add new fund")
def add_fund():
fund = st.selectbox("Name Fund", options=df2["Name Fund"].unique())
weight = st.select_slider(
"Weight", options=[0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]
)
asset_class = df2[df2["Name Fund"] == fund]["Asset Class"].values[0]
if st.button("Add"):
st.session_state["data"] = pd.concat(
[
st.session_state["data"],
pd.DataFrame(
{
"Name Fund": [fund],
"Asset Class": [asset_class],
"Weight": [weight],
}
),
],
ignore_index=True,
)
st.rerun()
if st.button("Add new row"):
add_fund()
1 Like
Thanks !
How to change also the value of weights inside the dataframe ie I mean I would like also change the weight value and also add new rows (and you gave me the solution to this)
Do you mean something like this?
import pandas as pd
import streamlit as st
if "data" not in st.session_state:
st.session_state["data"] = pd.DataFrame(
{
"Name Fund": ["Fund A", "Fund B", "Fund C"],
"Asset Class": ["Equity", "Fixed Income", "Commodity"],
"Weight": [0.4, 0.3, 0.3],
}
)
data2 = {
"Name Fund": ["Fund D", "Fund E", "Fund F"],
"Asset Class": ["Equity", "Fixed Income", "Commodity"],
}
@st.experimental_dialog("Edit weights")
def edit_weights():
edited_data = st.data_editor(
st.session_state["data"], hide_index=True, disabled=("Name Fund", "Asset Class")
)
if st.button("Save"):
st.session_state["data"] = edited_data
st.rerun()
# Créer le DataFrame
df2 = pd.DataFrame(data2)
# Afficher le DataFrame
st.dataframe(st.session_state["data"], hide_index=True)
@st.experimental_dialog("Add new fund")
def add_fund():
fund = st.selectbox("Name Fund", options=df2["Name Fund"].unique())
weight = st.select_slider(
"Weight", options=[0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]
)
asset_class = df2[df2["Name Fund"] == fund]["Asset Class"].values[0]
if st.button("Add"):
st.session_state["data"] = pd.concat(
[
st.session_state["data"],
pd.DataFrame(
{
"Name Fund": [fund],
"Asset Class": [asset_class],
"Weight": [weight],
}
),
],
ignore_index=True,
)
st.rerun()
if st.button("Edit weights"):
edit_weights()
if st.button("Add new row"):
add_fund()
1 Like
system
Closed
June 18, 2024, 11:05am
9
This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.