gpt4 book ai didi

python - Pandas:if 语句有错误: 'single positional indexer is out-of-bounds' then else

转载 作者:行者123 更新时间:2023-12-01 01:04:25 25 4
gpt4 key购买 nike

我想做的是:

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()

知道如何做到这一点吗?谢谢!

最佳答案

您可以将 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()))

关于python - Pandas:if 语句有错误: 'single positional indexer is out-of-bounds' then else,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55515941/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com