I have very similar dataframe as below:
Date Col1 Col2 Col3 ...
2020/11/04 -10 0 12
2020/11/05 31 12 42
2020/11/07 10 1 -12
2020/11/08 2 -15 1
2020/11/09 2 10 0
.
.
.
My cumsum condition is while calculating sum for next row if sum is negative change it to 0. Output of the operation should look like below.
Date Col1 Col2 Col3 ...
2020/11/04 0 0 12
2020/11/05 31 12 54
2020/11/07 41 13 42
2020/11/08 43 0 43
2020/11/09 45 10 43
.
.
.
I have achieved this by looping and applying condition through each rows and column but for cumbersome data its performance is very poor.
columns = diff.columns
for col in columns:
if diff.iloc[0].at[col] < 0:
diff.iloc[0].at[col] = 0
for i,row in diff.iterrows():
if not i == diff.first_valid_index():
prev = diff.index.get_loc(i) - 1
for col in columns:
diff.loc[i].at[col] = diff.loc[i].at[col] + diff.iloc[prev].at[col]
if diff.loc[i].at[col] < 0:
diff.loc[i].at[col] = 0
How can I do it better way in pandas?
UPDATE this thread here is very relevant and my solution is:
def adj_func(x):
total = 0
result = []
for i, y in enumerate(x):
total += y
if total < 0:
total = 0
result.append(total)
return result
diff[col_list].apply(adj_func)