0

I have a dataframe like this

          Date  year  month  day  Week days  % Change
0   2020-01-02  2020      1    2   Thursday       NaN
1   2020-01-03  2020      1    3     Friday     -0.71
2   2020-01-06  2020      1    6     Monday      0.35
3   2020-01-07  2020      1    7    Tuesday     -0.28
4   2020-01-08  2020      1    8  Wednesday      0.49
..         ...   ...    ...  ...        ...       ...
500 2021-12-27  2021     12   27     Monday      1.38
501 2021-12-28  2021     12   28    Tuesday     -0.10
502 2021-12-29  2021     12   29  Wednesday      0.14
503 2021-12-30  2021     12   30   Thursday     -0.30
504 2021-12-31  2021     12   31     Friday     -0.26

Given a specific day over the data series (1,2, 3 etc), I need to calculate the cumulative value of %change column and the mean value. The result should be memorized in a new data frame with 31 rows (from day 1 to day 31, days of months) and 2 column, the mean value and cumulative value

I tried this code in order to verify if i calculate correctly cumulative and mean value

Range_Days = [*range(1, 32, 1)]
Perf=Range_Days
Perf_mean=Range_Days
i=0
    
for x in Range_Days:
     uno=Ticker_Ch.loc[Ticker_Ch["day"]==x]
     Perf_mean[i]=Ticker_Ch.loc[Ticker_Ch["day"]==x,'% Change'].mean().round(4)
     Perf[i]=Ticker_Ch.loc[Ticker_Ch["day"]==x,'% Change'].sum().round(4)
     i=i+1

print(Perf_mean)
print(Perf)
#Perf_day_s_month=pd.DataFrame(Perf,Perf_mean)

but Perf_mean and Perf seems have the same values as you can se below

[*********************100%***********************]  1 of 1 completed
[1.89, 13.5, -4.71, 5.57, 12.23, 8.94, 5.89, 6.28, -2.14, 3.6, -11.34, -7.87, 7.87, 7.12, 5.52, -9.64, 5.58, -9.13, -3.47, -2.26, -1.33, 6.1, -0.86, 9.27, 0.26, 2.8, -7.03, -1.54, 7.17, 
-1.22, -2.21]
[1.89, 13.5, -4.71, 5.57, 12.23, 8.94, 5.89, 6.28, -2.14, 3.6, -11.34, -7.87, 7.87, 7.12, 5.52, -9.64, 5.58, -9.13, -3.47, -2.26, -1.33, 6.1, -0.86, 9.27, 0.26, 2.8, -7.03, -1.54, 7.17, 
-1.22, -2.21]

Since if i print Perf_mean and Perf inside the loop the puntual result is corrent,what I'm doing wrong?

Mario V
  • 25
  • 2
  • Basically the same problem as https://stackoverflow.com/questions/240178/list-of-lists-changes-reflected-across-sublists-unexpectedly You have multiple references to the same list object. – Michael Butscher Apr 24 '23 at 16:25
  • `Perf=Range_Days ; Perf_mean=Range_Days` this creates 2 names pointing to the same object. – mozway Apr 24 '23 at 16:44

0 Answers0