简体   繁体   中英

How to solve a Keyerror in python for-loop that occurs when reaching the end of the loop

I am creating a new column in an existing df with the values of the previous column in the i+1 position. This means the last value of the new column will remain empty.

The code is working in the sense the new df is correctly created but it is giving Key Error. I udnerstand the loop is not detecting how to stop. Seems to have a pretty simple solution but I have not been able to fix it so far. This is the code.

data = [['scn1', 100, ''], ['scn1', 150, ''], ['scn1', 300, '']]    
OCS = pd.DataFrame(data, columns = ['scn', 'FROM', 'TO'])

for i in OCS.index:
        a = i
        i = i+1
        lastvld = OCS.loc[i].FROM  
        maxloop = len(OCS)
        if a <= maxloop:
            OCS.loc[a, 'TO'] = lastvld  

Any help will be very appretiated.

If the KeyError is raised from a failed dictionary key lookup in your own code, you can use.get() to return either the value found at the specified key or a default value.....or try using raise KeyError(message)

The problem is the line i = i+1 . The for loop will update i with each value in the index, so you don't need to do this at all. Incrementing i inside the loop is assigning a value that is not in your index.

I think what you're trying to do is set the TO column to the FROM value in the next row, for all but the last row. Try this:

import pandas as pd

data = [['scn1', 100, ''], ['scn1', 150, ''], ['scn1', 300, '']]
OCS = pd.DataFrame(data, columns = ['scn', 'FROM', 'TO'])

maxloop = OCS.index.max()

for i in OCS.index:
    if i < maxloop:
        lastvld = OCS.loc[i+1].FROM
        OCS.loc[i, 'TO'] = lastvld

print(OCS)

Output:

    scn  FROM   TO
0  scn1   100  150
1  scn1   150  300
2  scn1   300  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM