0

When my script doesn't find an specific cell in excel file it throws an error and stops entire scripts. I don't want this to happen!

'Total Employees':  '{}'.format(df.iloc[1]['5A'])

And error massage from console:

Traceback (most recent call last):
  File "c:\Users\szyma\Desktop\python_shit\index.py", line 25, in <module>
    'Total Employees':  '{}'.format(df.iloc[row_iterator]['5A']),
  File "C:\Python39\lib\site-packages\pandas\core\series.py", line 824, in __getitem__
    return self._get_value(key)
  File "C:\Python39\lib\site-packages\pandas\core\series.py", line 932, in _get_value
    loc = self.index.get_loc(label)
  File "C:\Python39\lib\site-packages\pandas\core\indexes\base.py", line 3082, in get_loc
    raise KeyError(key) from err
KeyError: '5A'

Instead, I want to paste in curly braces for example 'empty'. How can I achieve this?

Adam Szymański
  • 335
  • 4
  • 17

2 Answers2

1

'Total Employees': '{}'.format(df.iloc[1].get('5A', 'empty'))

szatkus
  • 1,292
  • 6
  • 14
1
try:
    print('Total Employees :  {}'.format(df.iloc[1]['5A']))
except KeyError:
    print("Total Employees : empty")
TheEagle
  • 5,808
  • 3
  • 11
  • 39