St.data_editor import from list

Hi,
i would like to know if it is possible with st.data_editor to add data from a other list ?
i mean i have a dataframe:

import streamlit as st
import pandas as pd

df = pd.DataFrame(
    [
       {"Asset": "Asset 1", "Caract" : 'Equity", "weight": 35.0}
       {"Asset": "Asset 2", "Caract" : 'Equity", "weight": 35.0}
       {"Asset": "Asset 3", "Caract" : 'Bonds", "weight": 10.0}
       {"Asset": "Asset 4", "Caract" : 'Gold", "weight": 35.0}
   ]

st.data_editor(df)

I would like to add asset from an other dataframe (df_complete for example) with data filed automatically when i add “Asset 5” for example ?

What do you mean by that?

I mean from an other list, when I started to add an asset it will search in this list and propose me the right code
Also when I validate the choice I would like to have the price of the asset import in the new row of the data frame.

Cordialement

How that asset would look like?

I mean I have a df that looks like:

import streamlit as st
import pandas as pd

df = pd.DataFrame(
    [
       {"Asset": "Asset 1", "Caract" : 'Equity", "price" : 10, "weight": 35.0}
       {"Asset": "Asset 2", "Caract" : 'Equity", "price" : 5, "weight": 35.0}
       {"Asset": "Asset 3", "Caract" : 'Bonds", "price" : 100, "weight": 10.0}
       {"Asset": "Asset 4", "Caract" : 'Gold", "weight": 35.0}
   ]

st.data_editor(df)

I would like from an other data frame to add a line ie the other df is like that for example:

df = pd.DataFrame(
    [
       {"Asset": "Asset 5", "Caract" : 'Equity", "price" : 10}
       {"Asset": "Asset 6", "Caract" : 'Equity", "price" : 5}
       {"Asset": "Asset 7", "Caract" : 'Bonds", "price" : 100}
       {"Asset": "Asset 8", "Caract" : 'Gold"}
   ]

You can add a dataframe to another dataframe this way.

import pandas as pd

df1 = pd.DataFrame([
    {"Asset": "Asset 1", "Caract": "Equity", "price": 10, "weight": 35.0},
    {"Asset": "Asset 2", "Caract": "Equity", "price": 5, "weight": 35.0},
    {"Asset": "Asset 3", "Caract": "Bonds", "price": 100, "weight": 10.0},
    {"Asset": "Asset 4", "Caract": "Gold", "price": None, "weight": 35.0}
])

df2 = pd.DataFrame([
    {"Asset": "Asset 5", "Caract": "Equity", "price": 10, "weight": None},
    {"Asset": "Asset 6", "Caract": "Equity", "price": 5, "weight": None},
    {"Asset": "Asset 7", "Caract": "Bonds", "price": 100, "weight": None},
    {"Asset": "Asset 8", "Caract": "Gold", "price": None, "weight": None}
])

df = pd.concat([df1, df2], ignore_index=True)

st.data_editor(df)

It is not what I wanted.
I want to write the df1 data frame and to add new lines from df1 (with st.data_editor(df1)). With data from df2

Cordialement

There is an example df1 and df2. Could you write the output that you want?

I would like in first instance print the df1 dataframe but to add data it should take the data from df2. Then print the new df1 data frame with the new rows inserted from df2.
Is that clearer ?

import streamlit as st
import pandas as pd

df1 = pd.DataFrame([
    {"Asset": "Asset 1", "Caract": "Equity", "price": 10, "weight": 35.0},
    {"Asset": "Asset 2", "Caract": "Equity", "price": 5, "weight": 35.0},
    {"Asset": "Asset 3", "Caract": "Bonds", "price": 100, "weight": 10.0},
    {"Asset": "Asset 4", "Caract": "Gold", "price": None, "weight": 35.0}
])

df2 = pd.DataFrame([
    {"Asset": "Asset 5", "Caract": "Equity", "price": 25, "weight": None},
    {"Asset": "Asset 6", "Caract": "Equity", "price": 15, "weight": None},
    {"Asset": "Asset 7", "Caract": "Bonds", "price": 45, "weight": None},
    {"Asset": "Asset 8", "Caract": "Gold", "price": None, "weight": None}
])

# Show df1
st.write('df1')
st.data_editor(df1)

# Add all rows of df2 to df1
new_df = pd.concat([df1, df2], ignore_index=True)
# df1 = pd.concat([df1, df2], ignore_index=True)  # to update df1
st.write('df1 + df2')
st.data_editor(new_df)

# Add the last row of df2 to df1
last_row_df2 = df2.iloc[[-1]]
new_df = pd.concat([df1, last_row_df2], ignore_index=True)
st.write('df1 + last row of df2')
st.data_editor(new_df)

# Add the first row of df2 to df1
first_row_df2 = df2.iloc[[0]]
new_df = pd.concat([df1, first_row_df2], ignore_index=True)
st.write('df1 + first row of df2')
st.data_editor(new_df)

# Add the third row of df2 to df1
third_row_df2 = df2.iloc[[2]]
new_df = pd.concat([df1, third_row_df2], ignore_index=True)
st.write('df1 + third row of df2')
st.data_editor(new_df)

# Add the first and second rows of df2 to df1
added_row_df2 = df2.iloc[[0, 1]]
new_df = pd.concat([df1, added_row_df2], ignore_index=True)
st.write('df1 + first and second rows of df2')
st.data_editor(new_df)

I am sorry but it is not what i would like to do !

I have a dataframe df1:

df1 = pd.DataFrame([
    {"Asset": "Asset 1", "Caract": "Equity", "price": 10, "weight": 35.0},
    {"Asset": "Asset 2", "Caract": "Equity", "price": 5, "weight": 35.0},
    {"Asset": "Asset 3", "Caract": "Bonds", "price": 100, "weight": 10.0},
    {"Asset": "Asset 4", "Caract": "Gold", "price": None, "weight": 35.0}
])

I would like to add rows that i want from another dataframe df2 and streamlit will complete the columns missing from df2 like ine the copied screen

Capture d’écran 2024-04-08 144709

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.