vendredi 17 septembre 2021

Python If And statement throws KeyError 9 in Dataframe

I'm trying to filter a df column and rename certain rows depending on next/previous row value.

enter image description here

This code (without AND though) worked for me for other sorting in the same df:

for row in df.index:
    if df['col1'][row] == 'b' and df['col1'][row+1] =='name':
        df['col1'][row +1] = 'name1' 
        df['col1'][row +2] = 'name2'

Here it throws KeyError 9 and I assume it's because of AND condition in row+1. Is there any way to workaround?

Aucun commentaire:

Enregistrer un commentaire