I am new to pandas so please bear with me. I have a matrix that looks like this:
df=
A B C D E F G H I J K
0 . . . . . . X L . . .
1 . . . . . . X A . . .
.
.
.
300 . . . . . . X R . . .
301 . . . . . . nan R . . .
302 . . . . . . X R . . .
303 . . . . . . nan R . . .
.
.
.
I am trying to change R to L if it's preceded by X and to N if preceded by nan (only these values can appear before R), this is what i expect as output :
A B C D E F G H I J K
0 . . . . . . X L . . .
1 . . . . . . X A . . .
.
.
.
300 . . . . . . X L . . .
301 . . . . . . nan N . . .
302 . . . . . . X L . . .
303 . . . . . . nan N . . .
.
.
.
this is my code:
slice = df.loc[df['H'] == 'R']
for i in range(len(slice)):
if isinstance([i,6],str):
slice.iloc[i,7]= 'L'
else:
slice.iloc[i,7]= 'N'
The output of df is the exact same as before the operation. Printing out slice shows that the cells were updated but the changes didn't affect df. I checked the pandas documentation and searched for many similar situations and all sources say iloc/loc return a view and not a copy yet the values are not being changed in the original DataFrame. I spent hours looking for the answer but in vain. What am i missing ?
I know I can loop through the whole set but I like to avoid do that since the dataset is huge and I am sure there is a more efficient way to do it.