[英]Python Pandas: Rolling backward function
I have a dataframe which has two columns (ie audit_value and rolling_sum).我有一个包含两列(即审计值和滚动总和)的数据框。 Rolling_sum_3 column contains the rolling sum of last 3 audit values.
Rolling_sum_3 列包含最后 3 个审计值的滚动总和。 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.现在我想对rolling_sum_3列应用条件并查找该值是否大于5,如果是,则查看audit_value的最后3个值并找到大于3的值。如果最后3个值中的任何值audit_value 的值大于 3,然后用 3 替换这些值并放置在新列中(称为 fixed_audit),否则在新列中保留 audit_value 的旧值。 I couldn't find any builtin function in pandas that perform rolling back functionality.
我在 Pandas 中找不到任何执行回滚功能的内置函数。 Could anyone suggest easy and efficient way of performing rolling back functionality on certain column?
任何人都可以建议在某些列上执行回滚功能的简单有效的方法吗?
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']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.