user2293224
user2293224

Reputation: 2220

Python Pandas: Rolling backward function

I have a dataframe which has two columns (i.e. audit_value and rolling_sum). Rolling_sum_3 column contains the rolling sum of last 3 audit values. Dataframe is shown below:

df1

        audit_value  rolling_sum_3  Fixed_audit
0       4             NA              3
1       5             NA              3
2       3             12              3
3       1             9               1
4       2             6               2
5       1             4               1
6       4             7               3

Now I want to apply condition on rolling_sum_3 column and find if the value is greater than 5, if yes, then look at the last 3 values of audit_value and find the values which are greater than 3. If the any value among the last 3 values of audit_value is greater than 3 then replace those value with 3 and place in a new column (called fixed_audit), otherwise retain the old value of audit_value in new column. I couldn't find any builtin function in pandas that perform rolling back functionality. Could anyone suggest easy and efficient way of performing rolling back functionality on certain column?

Upvotes: 1

Views: 93

Answers (1)

jlewkovich
jlewkovich

Reputation: 2775

df1['fixed_audit'] = df1['audit_value']
for i in range(3, len(df1)):
    if(df1.iloc[i].rolling_sum_3 > 5):
        df1.loc[i-1,'fixed_audit'] = 3 if df1.loc[i-1,'audit_value'] > 3 else df1.loc[i-1,'audit_value']
        df1.loc[i-2,'fixed_audit'] = 3 if df1.loc[i-2,'audit_value'] > 3 else df1.loc[i-2,'audit_value']
        df1.loc[i-3,'fixed_audit'] = 3 if df1.loc[i-3,'audit_value'] > 3 else df1.loc[i-3,'audit_value']

Upvotes: 2

Related Questions