循环问题:IndexError:单个位置索引器超出范围

时间:2019-12-03 13:20:15

标签: pandas loops dataframe

从我的代码中可以看到,我的最后一个if语句中有一个问题。当它到达数据帧的末尾时,就没有下一个要比较的值。如何轻松解决此问题?我已经查询过类似的问题,但没有直接适用于我的案例。我还尝试将最后一个if语句添加到以下内容:(i

for i in range(1, len(df)):

    if (df.iloc[i]['loc_ID'] == df.iloc[i-1]['loc_ID']) and (df.iloc[i]['Weeknum_start'] == df.iloc[i]['Weeknum_end']):
        df.iloc[i,df.columns.get_loc('Aantal unieke schepen')] =  df.iloc[i-1]['Tijd lage bezetting']
    if(df.iloc[i]['occupiedPercentage'] < 40):
        if(df.iloc[i]['Weeknum_start'] == df.iloc[i]['Weeknum_end']):
                df.iloc[i,df.columns.get_loc('Aantal unieke schepen')] += df.iloc[i]['diff']
        else:
                df.iloc[i,df.columns.get_loc('Aantal unieke schepen')] += df.iloc[i]['endedAtDecimals'] -  df.iloc[i]['endedAtInteger']
    if(df.iloc[i+1]['Weeknum_start'] != df.iloc[i+1]['Weeknum_end']):
        if(df.iloc[i+1]['occupiedPercentage'] < 40):
            df.iloc[i,df.columns.get_loc('Aantal unieke schepen')] += df.iloc[i+1]['endedAtInteger'] - df.iloc[i+1]['startedAtDecimals']

0 个答案:

没有答案