熊猫:如果语句出错:“单个位置索引器超出范围”,则其他

时间:2019-04-04 12:23:59

标签: python pandas

我想做的是以下事情:

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

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

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

关于如何做到这一点的任何想法?谢谢!

1 个答案:

答案 0 :(得分:2)

您可以将if-else语句与Series.empty一起使用:

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

或者将nextiter一起使用-如果Series为空,则可以设置默认值:

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