[英]How to add condition to rolling window?
I have the following pandas data frame df
:我有以下熊猫数据框
df
:
COL1 COL2 COL3 Y
10 2 A 1
20 5 A 3
30 2 B 1
20 7 B 4
15 2 A 2
25 1 B 1
10 3 A 1
25 1 A 1
I apply rolling to y
as follows:我将滚动应用于
y
如下:
window = 2
y = df["Y"]
y = y.rolling(window).apply(lambda x: np.max(x) if len(x)>0 else 0).dropna()
But now I need to add a restriction to y
: the max
should be calculated only over rows where COL3
is equal to A
.但是现在我需要对
y
添加一个限制: max
应该只在COL3
等于A
行上计算。 If there is no A
value in rows, then y
should be equal to 0. For example, rows 3 and 4 (if we use the window
of 2)如果行中没有
A
值,则y
应该等于0。例如第3行和第4行(如果我们使用2的window
)
I tried:我试过:
y = df.rolling(window).apply(lambda row: np.max(row[row["COL3"=="A"]]["Y"]) if len(row["Y"])>0 else 0).dropna()["Y"]
But got the error:但是得到了错误:
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices
We can split the y before the rolling
and reindex
fill the value with 0我们可以在
rolling
之前拆分 y 并用 0 reindex
填充值
y1 = y[df.COL3 == 'A']
y1 = y1.rolling(window).apply(lambda x: np.max(x) if len(x)>0 else 0).fillna('drop')
y = y1.reindex(y.index, fill_value = 0).loc[lambda x : x!='drop']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.