I have a Dataframe at hand. After making predictions, I want to update that dataframe with these values. I know there are similar questions, and answers around this site. I tried all of them. But I couldn't succeed. It gives the warning of "A value is trying to be set on a copy of a slice from a DataFrame." and do not update the original dataframe actually.
According to the questions similar to my problem, I tried changing the code with iloc
, loc
, iat
etc. Nothing has worked.
My code takes a subdataframe from pred_set with filter, then makes predictions rowwise in order. Prediction of the upper row must update cells of lower rows. So that, i am making future predictions with moving months.
for key in list(mi.unique_everseen(pred_set['from'] + pred_set['to'])):
for i in range(0, predmonths):
pred = pred_set[(pred_set['from'] + pred_set['to']) == key].iloc[[i],:]
pred_val = all_pipe_final.predict(pred)
for j in range (i+1, predmonths):
pred_set.loc[(pred_set['from'] + pred_set['to']) == key, :].iloc[j, pred_set.columns.get_loc(str('M-'+str(j)))] = pred_val[0]
#variation 2#pred_set[(pred_set['from'] + pred_set['to']) == key].iloc[j, pred_set.columns.get_loc(str('M-'+str(j)))] = pred_val[0]
#variation 3#pred_set[(pred_set['from'] + pred_set['to']) == key].iloc[[j], [pred_set.columns.get_loc(str('M-'+str(j)))]] = pred_val[0]
As I stated before, this code part and variations do not update the cells. It looks like, i am working on a copy of dataframe, not the original one. What is the reason of that, I am wondering.
Eventually I changed the structure and added:
pred_subset = pred_set[(pred_set['from'] + pred_set['to']) == key]
I recorded calculations on this new dataframe, it worked. I don't know the reason.