I’m trying to update the column names of a dataframe, based on a second dataframe:
# Rest of the code
def init_data_overview(self):
self.data_tab, self.metadata_tab = self.tab1.tabs(["Data", "Metadata"])
if 'data' not in st.session_state:
st.session_state.data, st.session_state.metadata = self.sql.load_data(st.session_state.data_selectbox)
if len(st.session_state.data_select_box_options) > 0 and st.session_state.data_selectbox != None:
self.data_tab.data_editor(st.session_state.data, use_container_width=True, key="data_editor")
self.metadata_tab.data_editor(st.session_state.metadata, use_container_width=True, key="metadata_editor", on_change=self.update_data_by_metadata)
def update_data_by_metadata(self):
if st.session_state.metadata_editor['edited_rows']:
# Create a copy of the DataFrame
data_copy = st.session_state.data.copy()
for index, changes in st.session_state['metadata_editor']['edited_rows'].items():
if 'Variable' in changes:
# Update the column name in the copy
data_copy.columns.values[index] = changes['Variable']
# Update the session state with the modified DataFrame
st.session_state.data = data_copy
# Rest of the code
If I update the column names, I get an ‘key error’:
File “…\MAIN.py”, line 31, in init
self.init_data_overview()
File “…\MAIN.py”, line 45, in init_data_overview
self.data_tab.data_editor(st.session_state.data, use_container_width=True, key=“data_editor”)
File “….venv\Lib\site-packages\streamlit\runtime\metrics_util.py”, line 397, in wrapped_func
result = non_optional_func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “….venv\Lib\site-packages\streamlit\elements\widgets\data_editor.py”, line 812, in data_editor
apply_data_specific_configs(
File “c:\Users\Yannick\Programmier Projekte\AnalyzeEverything.venv\Lib\site-packages\streamlit\elements\lib\column_config_utils.py”, line 492, in apply_data_specific_configs
for column_name, column_data in data_df.items():
File “….venv\Lib\site-packages\pandas\core\frame.py”, line 1500, in items
yield k, self._get_item_cache(k)
^^^^^^^^^^^^^^^^^^^^^^^
File “….venv\Lib\site-packages\pandas\core\frame.py”, line 4638, in _get_item_cache
loc = self.columns.get_loc(item)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File “….venv\Lib\site-packages\pandas\core\indexes\base.py”, line 3812, in get_loc
raise KeyError(key) from err
KeyError: ‘NewVariableName’
The error is thrown after I update the dataframes column names in this line of code:
self.data_tab.data_editor(st.session_state.data, use_container_width=True, key="data_editor")
I don’t certainly know if this is a bug, or if I just don’t see my mistake.
Help would be very much appreciated!
- Streamlit version: 1.33.0
- Python version: 3.12.1
- Operating System: Windows 11 Home
- Browser: Opera GX