Dear sir I’m trying to select multiple rows and columns at once to create a new DataFrame, but I’m running into errors with my current code. I need help to get the right code for selecting multiple columns and rows simultaneously. Thank you in advance for your assistance!
see my code
import streamlit as st
import pandas as pd
import numpy as np
if “df” not in st.session_state:
st.session_state.df = pd.DataFrame(
np.random.randn(12, 5), columns=[“a”, “b”, “c”, “d”, “e”]
)
event = st.dataframe(
st.session_state.df,
key=“data”,
on_select=“rerun”,
selection_mode=[“multi-row”, “multi-column”],
)
event.selection
df_selected = st.session_state.df.loc[event.selection[‘rows’][‘columns’]] # is give me error
#df_selected = st.session_state.df.loc[event.selection[‘rows’],[‘columns’]] # is give me error
#df_selected = st.session_state.df.loc[event.selection[‘rows’,‘columns’]] # is also give the error
#df_selected = st.session_state.df.loc[event.selection[‘rows’]] # this run ok
st.write(df_selected)
see error
KeyError: “None of [Index([‘columns’], dtype=‘object’)] are in the [columns]”
Traceback:
File "C:\Users\Ali\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\streamlit\runtime\scriptrunner\exec_code.py", line 88, in exec_func_with_error_handling
result = func()
^^^^^^File "C:\Users\Ali\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\streamlit\runtime\scriptrunner\script_runner.py", line 579, in code_to_exec
exec(code, module.__dict__)File "C:\mfa\singandmultirowcol.py", line 20, in <module>
df_selected = st.session_state.df.loc[event.selection['rows'],['columns']] # is give me error
~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^File "C:\Users\Ali\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\pandas\core\indexing.py", line 1184, in __getitem__
return self._getitem_tuple(key)
^^^^^^^^^^^^^^^^^^^^^^^^File "C:\Users\Ali\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\pandas\core\indexing.py", line 1375, in _getitem_tuple
return self._multi_take(tup)
^^^^^^^^^^^^^^^^^^^^^File "C:\Users\Ali\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\pandas\core\indexing.py", line 1327, in _multi_take
axis: self._get_listlike_indexer(key, axis)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^File "C:\Users\Ali\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\pandas\core\indexing.py", line 1558, in _get_listlike_indexer
keyarr, indexer = ax._get_indexer_strict(key, axis_name)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^File "C:\Users\Ali\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\pandas\core\indexes\base.py", line 6200, in _get_indexer_strict
self._raise_if_missing(keyarr, indexer, axis_name)File "C:\Users\Ali\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\pandas\core\indexes\base.py", line 6249, in _raise_if_missing
raise KeyError(f"None of [{key}] are in the [{axis_name}]")
import streamlit as st
import pandas as pd
import numpy as np
if “df” not in st.session_state:
st.session_state.df = pd.DataFrame(
np.random.randn(12, 5), columns=[“a”, “b”, “c”, “d”, “e”]
)
event = st.dataframe(
st.session_state.df,
key=“data”,
on_select=“rerun”,
selection_mode=[“multi-row”, “multi-column”],
)
event.selection
df_selected = st.session_state.df.loc[event.selection[‘rows’][‘columns’]] # is give me error
#df_selected = st.session_state.df.loc[event.selection[‘rows’],[‘columns’]] # is give me error
#df_selected = st.session_state.df.loc[event.selection[‘rows’,‘columns’]] # is also give the error
#df_selected = st.session_state.df.loc[event.selection[‘rows’]] # this run ok
st.write(df_selected)