I am setting a column based on a list of indices, which I computed earlier. I also deleted some indices during computations, so some indices are no longer part of my dataframe. This simplified example shows what's happening now:
df = pd.DataFrame(data=[[1 , 2, False], [4 , 5, False]], columns=["col1", "col2", "col3"], index=[1, 3])
df.loc[[1, 2], 'col3'] = True
Obviously I am getting an KeyError:
KeyError: '[2] not in index'
Unfortunately .loc() does not provide something like error="ignore". Would be iterating over the list catching the KeyError with a try-catch-block the only solution or is there a nicer way to do that?
Any hints? Thanks