I am running a query for a dataframe with a column of type String that returns rows that contain a certain the world ‘select’:
df.query('code.str.contains("select")')
However, when I try to run the same query in the pandasgui as a filter that takes a query expression, it is listed as a failed filter and does not evaluate it.
Picture of the query in pandasgui and columns for the dataframe
I tried a combination of "
or '
around contains, and no ` around the column name but can’t seem to find a method that enables the filter. Does anyone know if there’s a way to query a column in a dataframe through pandasgui to search a string value for a specific substring? I’d appreciate any help!
I also ended up searching through the pandasgui source code, and a good starting point would be in the pandasgui/store.py
file, where the authors used the apply_filters()
method in numerous places. It looks to me like the query is considered failed after catching an exception in the apply_filters()
method, which is where self.filters[index].failed
is set to True from False.
Edit: It should be noted that boolean expressions like code == 'select'
works, but only returns rows that are exactly ‘select’. I am looking to find rows that contain the word ‘select’ with a query in the filters tab of pandasgui.
@status_message_decorator("Applying filters...")
def apply_filters(self):
df = self.df_unfiltered.copy()
df['_temp_range_index'] = df.reset_index().index
for ix, filt in enumerate(self.filters):
if filt.enabled and not filt.failed:
try:
df = df.query(filt.expr)
# Handle case where filter returns only one row
if isinstance(df, pd.Series):
df = df.to_frame().T
except Exception as e:
self.filters[ix].failed = True
logger.exception(e)
# self.filtered_index_map is used elsewhere to map unfiltered index to filtered index
self.filtered_index_map = df['_temp_range_index'].reset_index(drop=True)
df = df.drop('_temp_range_index', axis=1)
self.df = df
self.data_changed()
swft is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
0