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 StuffPy ProjectsAppDF_SearchfileSearch.py", line 28, in <module>
print(tabulate(df_f, headers='keys', tablefmt='simple_grid', maxcolwidths=[None, None, 100]))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "F:PythonLibsite-packagestabulate__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 | Column 4 | Column 5 | Column 6 | Column 7 | Column 8 | Column 9 | Column 10 | Column 11 | Column 12 | Column 13 |
---|---|---|---|---|---|---|---|---|---|---|---|---|
ABC-12345 | Bill | Win | 1/6/1981 | 123 456 | 124 456 | 125 456 | 126 456 | 127 456 | 128 456 | 129 456 | 130 456 | 131 456 |
ABC-12346 | Tom | Window | 7/31/1980 | 789 10 11 12 | 790 10 11 12 | 791 10 11 12 | 792 10 11 12 | 793 10 11 12 | 794 10 11 12 | 795 10 11 12 | 796 10 11 12 | 797 10 11 12 |
ABC-12347 | Matt | Red | 1/13/2011 | 124 456 | 125 456 | 126 456 | 127 456 | 128 456 | 129 456 | 130 456 | 131 456 | 132 456 |
ABC-12348 | Jim | RedHat | 6/10/2000 | 790 10 11 12 | 791 10 11 12 | 792 10 11 12 | 793 10 11 12 | 794 10 11 12 | 795 10 11 12 | 796 10 11 12 | 797 10 11 12 | 798 10 11 12 |
ABC-12349 | Bill | RHEL | 1/6/1981 | 125 456 | 126 456 | 127 456 | 128 456 | 129 456 | 130 456 | 131 456 | 132 456 | 133 456 |
ABC-12350 | Tom | Primavera | 7/31/1980 | 791 10 11 12 | 792 10 11 12 | 793 10 11 12 | 794 10 11 12 | 795 10 11 12 | 796 10 11 12 | 797 10 11 12 | 798 10 11 12 | 799 10 11 12 |
ABC-12351 | Matt | Portable | 1/13/2011 | 126 456 | 127 456 | 128 456 | 129 456 | 130 456 | 131 456 | 132 456 | 133 456 | 134 456 |
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)
df = df[df.map(lambda x: isinstance(x, str) and searchterm.lower() in x.lower()).any(axis=1)] # this is the code that was provided in my previous question that works as expected when a string is found
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.”
What is being returned:
Enter search term: foo
Traceback (most recent call last):
File "f:Python StuffPy ProjectsAppDF_SearchfileSearch.py", line 28, in <module>
print(tabulate(df_f, headers='keys', tablefmt='simple_grid', maxcolwidths=[None, None, 100]))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "F:PythonLibsite-packagestabulate__init__.py", line 2054, in tabulate
num_cols = len(list_of_lists[0])
~~~~~~~~~~~~~^^^
IndexError: list index out of range
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')
Matt is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.