pythonpandasdataframe

When searching for strings in a dataframe via lambda function, how do I resolve the IndexError if no results are found in any column or row?


This question is piggy-backing off of a previous question that I asked regarding searching though a dataframe and printing out rows that contained a certain string, but not duplicating the output if the string value was found in more than one column.

Original question HERE.

The solution offered works unless the term being searched is not found. If not found I receive the following error:

Enter search term: foo
Traceback (most recent call last):
  File "f:\Python Stuff\Py Projects\App\DF_Search\fileSearch.py", line 28, in <module>
    print(tabulate(df_f, headers='keys', tablefmt='simple_grid', maxcolwidths=[None, None, 100]))
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "F:\Python\Lib\site-packages\tabulate\__init__.py", line 2054, in tabulate
    num_cols = len(list_of_lists[0])
                   ~~~~~~~~~~~~~^^^
IndexError: list index out of range

The data I am using is in the table below:

Column 1 Column 2 Column 3
ABC-12345 Bill Win
ABC-12346 Tom Window
ABC-12347 Matt Red

Not necessarily clean code, but I'm trying building dataframes from google sheets for the first time, so forgive me.

searchterm = input("Enter search term: ")
    sheet1_name = 'MySheetName' 
    sheet1_id = 'MyGoogleSheetID' 
    sheet1_url = f'https://docs.google.com/spreadsheets/d/{sheet1_id}/gviz/tq?tqx=out:csv&sheet={sheet1_name}'
    df = pd.read_csv(sheet1_url)
    # this is the code that was provided in my previous question that works as expected when a string is found
    df = df[df.map(lambda x: isinstance(x, str) and searchterm.lower() in x.lower()).any(axis=1)]
    print(tabulate(df, headers='keys', tablefmt='simple_grid', maxcolwidths=[None, None, 100]))

If a string is found anywhere in the dataframe the output is as expected.

When the searchterm is not found the expectation is for nothing to happen, although I would like there to be a printout of "No results."

I have tried adding an if statement which still shows the IndexError but it may not be in the correct position:

df_f = df_f[df_f.map(lambda x: isinstance(x, str) and searchterm.lower() in x.lower()).any(axis=1)]
if searchterm in searchterm.lower():
    print(tabulate(df_f, headers='keys', tablefmt='simple_grid', maxcolwidths=[None, None, 100]))
else:
    print('nope')

Solution

  • You can just check if the DF is empty before using the contents:

    if df.empty:
        print('not found')
    else:
        print(tabulate(df, headers='keys', tablefmt='simple_grid', maxcolwidths=[None, None, 100]))