Home > Mobile >  Dataframe doesn't modify in a for loop
Dataframe doesn't modify in a for loop

Time:02-17

I have this for loop and I would like to change the value of a part of a df.

for col in last_df:
    last_df.loc[last_df[col]!=0, col]='stop'
    stop=last_df[last_df[col]=='stop'][col].index[0]
    last_df[col].loc[:(stop-1)]='NaN' 

At the end, the last_df doesn't modify, the error I receive is the following: A value is trying to be set on a copy of a slice from a DataFrame See the caveats in the documentation:....

CodePudding user response:

Try without the loc.

for col in last_df:
        last_df[last_df[col]!=0]='stop'
        stop=last_df[last_df[col]=='stop'][col].index[0]
        last_df[col][:(stop-1)]='NaN' 

this is a issue with view and copy of the DataFrame for more information quick understand you can read in this link

  • Related