numpy / pandas 矢量化 "threshold crossing" 逻辑
numpy / pandas vectorized "threshold crossing" logic
我想实现这个 "threshold crossing" 逻辑(因为缺少更好的术语):
- 从第
num
列开始,第 switch
列设置为 1。
- 当
num
跌至(或超过)某个lower_bound
时,将switch
变为0。
- 保持
switch
为 0,直到 num
达到(或超过)upper_bound
,然后 switch
变回 1。
为了说明,这里是 lower_bound
3 和 upper_bound
6 的设置。
df = pd.DataFrame([6, 5, 3, 2, 4, 5, 6, 3, 7, 5], columns=['num'])
df['switch'] = 1
这是我想要的输出。
num switch
0 6 1
1 5 1
2 3 0
3 2 0
4 4 0
5 5 0
6 6 1
7 3 0
8 7 1
9 5 1
当然可以轻松迭代,但我正在寻找有关矢量化 (numpy / pandas) 方法的一些想法。谢谢。
我想你可以用
s=df.num.ge(6).astype(int)-df.num.le(3).astype(int)
s.mask(s==0).ffill().replace(-1,0).fillna(1)
0 1.0
1 1.0
2 0.0
3 0.0
4 0.0
5 0.0
6 1.0
7 0.0
8 1.0
9 1.0
Name: num, dtype: float64
这是一个基于 numpy
的方法,虽然有点冗长,但应该相当有效。
a = df.num
lw, up = 3, 6
pd.Series(
np.select([a.le(lw), a.ge(up)], [-1, 1], np.nan)
).ffill().clip(0, 1).fillna(1)
0 1.0
1 1.0
2 0.0
3 0.0
4 0.0
5 0.0
6 1.0
7 0.0
8 1.0
9 1.0
dtype: float64
我想实现这个 "threshold crossing" 逻辑(因为缺少更好的术语):
- 从第
num
列开始,第switch
列设置为 1。 - 当
num
跌至(或超过)某个lower_bound
时,将switch
变为0。 - 保持
switch
为 0,直到num
达到(或超过)upper_bound
,然后switch
变回 1。
为了说明,这里是 lower_bound
3 和 upper_bound
6 的设置。
df = pd.DataFrame([6, 5, 3, 2, 4, 5, 6, 3, 7, 5], columns=['num'])
df['switch'] = 1
这是我想要的输出。
num switch
0 6 1
1 5 1
2 3 0
3 2 0
4 4 0
5 5 0
6 6 1
7 3 0
8 7 1
9 5 1
当然可以轻松迭代,但我正在寻找有关矢量化 (numpy / pandas) 方法的一些想法。谢谢。
我想你可以用
s=df.num.ge(6).astype(int)-df.num.le(3).astype(int)
s.mask(s==0).ffill().replace(-1,0).fillna(1)
0 1.0
1 1.0
2 0.0
3 0.0
4 0.0
5 0.0
6 1.0
7 0.0
8 1.0
9 1.0
Name: num, dtype: float64
这是一个基于 numpy
的方法,虽然有点冗长,但应该相当有效。
a = df.num
lw, up = 3, 6
pd.Series(
np.select([a.le(lw), a.ge(up)], [-1, 1], np.nan)
).ffill().clip(0, 1).fillna(1)
0 1.0
1 1.0
2 0.0
3 0.0
4 0.0
5 0.0
6 1.0
7 0.0
8 1.0
9 1.0
dtype: float64