You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello everyone, I have a question regarding state management for dataframes.
Let's imagine that I have the following dataframe which I want to express as multiple distinct data editors which need to be maintained in sync.
# Original dataframe
original_df = pd.DataFrame(
{
'name':['A','B','C'],
'group':['Decentralized', 'Centralized','Decentralized'],
'start_year':['2010', '2015', '2020'],
'amount_of_heat':['120','110','105']
}
)
# First data editor
original_editor = mo.ui.experimental_data_editor(original_df[['name','group','start_year']])
# Decentralized data editor
decentralized_editor = mo.ui.experimental_data_editor(original_df[original_df['group'] == 'Decentralized'][['name','start_year','amount_of_heat']])
# Centralized data editor
centralized_editor = mo.ui.experimental_data_editor(original_df[original_df['group'] == 'Centralized'][['name','start_year','amount_of_heat']])
What is the best approach to keep all of the data editors be kept in sync ? And have their respective individual changes be merged back into original_df ? Some typical changes I could make are:
Change the name of a row
Change the group from "Centralized" to "Decentralized" (or vice-versa)
Change the value of 'amount_of_heat' of a row
Approach tested
I have tried up to now to leverage the formula mo.state() and a custom defined function which returns the original dataframe with the values corresponding to the columns and rows of the edited dataframe updated, but this hasn't been successful in syncing. For the purpose of example, here's the code.
# Define the getter and setter functions for the original_df
get_original_df, set_original_df = mo.state(original_df)
# Define the update function of the original_df
def update_original_df(original_df, small_df):
# Update in-place (aligns on index and columns automatically)
original_df.update(small_df)
return original_df
# Leverage the on_change attribute of the data editors
# Original data editor
original_editor = mo.ui.experimental_data_editor(get_original_df()[['name','group','start_year']], on_change=set_original_df(lambda df: update_original_df(get_original_df(), df)))
# Decentralized data editor
decentralized_editor = mo.ui.experimental_data_editor(get_original_df()[get_original_df()['group'] == 'Decentralized'][['name','start_year','amount_of_heat']], on_change=set_original_df(lambda df: update_original_df(get_original_df(), df)))
# Centralized data editor
centralized_editor = mo.ui.experimental_data_editor(get_original_df()[get_original_df()['group'] == 'Centralized'][['name','start_year','amount_of_heat']], on_change=set_original_df(lambda df: update_original_df(get_original_df(), df)))
This has unfortunately not been successful in maintaining consistency. Do you have any suggestion ? Thanks for the help !!
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello everyone, I have a question regarding state management for dataframes.
Let's imagine that I have the following dataframe which I want to express as multiple distinct data editors which need to be maintained in sync.
What is the best approach to keep all of the data editors be kept in sync ? And have their respective individual changes be merged back into original_df ? Some typical changes I could make are:
Approach tested
I have tried up to now to leverage the formula mo.state() and a custom defined function which returns the original dataframe with the values corresponding to the columns and rows of the edited dataframe updated, but this hasn't been successful in syncing. For the purpose of example, here's the code.
This has unfortunately not been successful in maintaining consistency. Do you have any suggestion ? Thanks for the help !!
Beta Was this translation helpful? Give feedback.
All reactions