Python 中的条件滚动总和



我正在从Excel中的数据分析过渡到Python,并且无法找到Python中等效代码的解决方案以在我的数据框中使用。要计算滚动总和列,我将使用公式 IF(C3=FALSE,0,(1+D2(((对于下面发布的表格(。在此示例中,只要> 20 列中的 Amount 值高于 20,就返回值 1,然后将其添加到其上方的金额。

我尝试在 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 |
+-------+--------+-------+-------------+

尝试使用迭代行:

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

最新更新