how to change a vale in subset of a dataframe

Viewed 25

I have written this code to sequentially drive subsets of a DataFrame (df) and set the last value of variable "last" to nan in each subsets(df_orig). the problem is when I let the last value of "last" to nan in df_orig it change the same value in df and so in next df_origs the nans repeats, any clue?

thanks,

df=pd.read_csv('df.csv')
for j in range(num):
        
    start_time=j
    end_time=train_candles+j
    df_orig = df.iloc[start_time:end_time,:]
    df_orig.iloc[-1, df_orig.columns.get_loc('last')] = np.nan 
1 Answers

I think, when you assign the df_orig then not only just the values but also the reference/index also get copied from df... So when you update np.nan in df_orig, it also changes in df...

Just make a small change in your code in the same line with .copy() in the end to avoid this issue... Like this;

df_orig = df.iloc[start_time:end_time,:].copy()

For more info, read about shallow vs deep copy in python... Hope this helps...

Related