So I have a dataframe with closing and opening prices, total length of 1066 rows. I want to iterate over the column 'signal' (values are available from 700th row onward, which I want to skip all the way), check if 1, then perform a calculation using the values from the corresponding columns, then compute cumulative sum in a new column. I want the code to perform the calculation only when 1 is checked.
let's say I have this dataframe:
(I don't know how to post a dataframe)
df = pd.DataFrame({'open':[2,4,5,7,5,11],
'close':[0,2,4,3,10,5],
'signal':[np.nan, 0, 1, -1, 1, -1]})
the desired results are as if the dataframe is constructed as follows:
df = pd.DataFrame({'open':[2,4,5,7,5,11],
'close':[0,2,4,3,10,5],
'signal':[np.nan, 0, 1, -1, 1, -1],
'profits':['','',-1,-1,4, 4]})
I have edited the code per the answer.
My code still ignore 1 in 'signal' and it just calculates the cumulative sum for the whole column, although transaction_count passes.
here is my code:
transaction_count = 0
for i in df['signal']:
if i == 1:
df['profits'] = (df['close'] - df['open']).cumsum()
transaction_count +=1
print('transaction count:', transaction_count)
print(df['profits_rf'])
[EDIT]
In this updated case you are mixing two answers. As @quest said, you dont need to loop. But if you want to do so anyways, you could create an array and append this array to your dataframe:
transaction_count = 0
profits = []
cum_sum = 0
for i in range(len(df)):
if df['signal'][i] == 1:
cum_sum += df['close'][i] - df['open'][i]
profits.append(cum_sum)
else:
profits.append(cum_sum)
df['profits'] = profits
[OLD]
This part seems to be the problem:
if i in df['signal'] == 1[i]:
You are already iterating over df['signal'] so you just could check if i == 1:
if i == 1:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.