I am transitioning from data analysis in Excel to Python and have not been able to find a solution for the equivalent code in Python to use in my data frame. To calculate the Rolling Sum column, I would use the formula IF(C3=FALSE,0,(1+D2)) (For the table posted below). In this example, as long as the Amount value is above 20 in the > 20 column, return the value of 1 and then add it to the amount above it.
My attempt to create the Rolling Sum column in Python:
def f(row):
if row['> 20'] == False:
val = 0
else:
#getting stuck here as to how to add to the row above, shift(1) is incorrect
val = 1 + shift(1)
return val
df['Rolling Sum'] = df.apply(f, axis=1)
Event | Amount | > 20 | Rolling Sum |
+-------+--------+-------+-------------+
| 1 | 7 | FALSE | |
| 2 | 25 | TRUE | 1 |
| 3 | 28 | TRUE | 2 |
| 4 | 3 | FALSE | 0 |
| 5 | 30 | TRUE | 1 |
| 6 | 35 | TRUE | 2 |
| 7 | 40 | TRUE | 3 |
| 8 | 6 | FALSE | 0 |
+-------+--------+-------+-------------+
try this with iterrows:
for index, row in df.iterrows():
if df.loc[index, '> 20'] == True:
df.loc[index, 'Rolling Sum'] = df.loc[index-1, 'Rolling Sum']+1
else:
df.loc[index, 'Rolling Sum'] = 0
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.