Viktor.w
Viktor.w

Reputation: 2297

Pandas: if statement with error: 'single positional indexer is out-of-bounds' then else

What I would like to do is the following:

if df.loc[df['proba'] <= proba_plus].iloc[0] is not single positional indexer is out-of-bounds

then: gamma_plus = df.loc[df['proba'] <= proba_plus].iloc[0]

else gamma_plus =df['gamma'].max()

Any idea on how to do that? Thanks!

Upvotes: 1

Views: 52

Answers (1)

jezrael
jezrael

Reputation: 862481

You can use if-else statement with Series.empty:

a = df.loc[df['proba'] <= proba_plus, 'gamma']
gamma_plus = df['gamma'].max() if a.empty else a.iat[0]

Or use next with iter - there is possible set default value if empty Series:

a = df.loc[df['proba'] <= proba_plus, 'gamma']
gamma_plus = next(iter(a, df['gamma'].max()))

Upvotes: 2

Related Questions