0

I have a filtered dataframe:

börse2 = bör[(bör['Ausprägung'] == bor)].iloc[0:1, : ]

which in some cases looks like this (empty):

enter image description here

When i run the command:

bor2 = börse2.iloc[0]['Schlüssel']

I get the error:

IndexError: single positional indexer is out-of-bounds

which makes sense, how can I see in this cases that bor2= 111 for example? Is there another option instead of try except. The problem with try except is that there can also be another error, but I want to explicity only use it if my dataframe bor2 is empty.

PV8
  • 5,799
  • 7
  • 43
  • 87
  • 1
    Can you not just use the empty method? [link](https://stackoverflow.com/questions/19828822/how-to-check-whether-a-pandas-dataframe-is-empty) – Monkey Pylot Feb 03 '20 at 09:30

1 Answers1

2

So if use boolean indexing:

börse2 = bör[(bör['Ausprägung'] == bor)]

And is necessary test empty dataframe use DataFrame.empty:

if börse2.empty:
    print ('no match')
else:
    print ('match')

But if want match some first value of column col if match, else some default value, here no match use next with iter trick:

s = bör.loc[(bör['Ausprägung'] == bor), 'col']
first = next(iter(s), 'no match')
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252