I have looked around (e.g. here), but I can't understand why my code is not working as expected. I have a pandas dataframe and I'd like to add a column that marks the last zero element in column B above a non-zero element.
df = pd.DataFrame({'B':[0,0,1,0,1,0,0,1]})
N = len(df.index)
df['C'] = N*[False]
for i in range(N-1):
if (df.iloc[i]['B']==0 and df.iloc[i+1]['B']>0):
df.iloc[i]['C']=True
In spite of having the condition satisfied 3 times, column C is still all false, and I also get a warning that I don't understand:
SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame
Any ideas?