如何使用 pandas 操作数组中的数据(并重置评估)
How to manipulate data in arrays using pandas (and resetting evaluations)
为了清楚起见,我修改了问题并删除了伪影和不一致之处 - 请重新打开以供社区考虑。一位贡献者已经认为将 groupby 与 cummax 结合使用可能是一种解决方案。
我有一个数据框,其中 col3 的先验值和 col2 的当前值之间的最大值是通过 Scott Boston 最近提供的 cummax 函数计算的(谢谢!),如下所示:
df['col3'] = df['col2'].shift(-1).cummax().shift().
生成的数据框如下所示。还添加了将 col2 与作为浮点类型值结果的设定值进行比较的所需逻辑。
运行cummax的结果:
col0 col1 col2 col3
0 1 5.0 2.50 NaN
1 2 4.9 2.45 2.45
2 3 5.5 2.75 2.75
3 4 3.5 1.75 2.75
4 5 3.1 1.55 2.75
5 6 4.5 2.25 2.75
6 7 5.5 2.75 2.75
7 8 1.2 0.6 2.75
8 9 5.8 2.90 2.90
希望在上例中当 col3 >= setpoint 或 2.71 时标记为 True,这样每次 col3 的最新行都超过 setpoint。
问题:当达到设定点时,cummax 解决方案不会重置。需要一个解决方案,在每次违反设定点时重置 cummax 计算。例如在上面的table中,在col3超过设定值时第一次True之后,即col2值为2.75,第二次应该满足相同的条件,即扩展数据中显示table 我在第 4 行中删除了 col3 的值,以说明需要“重置”cummax 计算。在 if 语句中,我使用下标 [-1] 来定位 df 中的最后一行(即最新行)。注意:col2=col1的当前值*constant1 where constant1 == 0.5
到目前为止尝试过的代码(注意 col3 没有正确重置):
if self.constant is not None: setpoint = self.constant * (1-self.temp) # suppose setpoint == 2.71
df = pd.DataFrame({'col0':[1,2,3,4,5,6,7,8,9]
,'col1':[5,4.9,5.5,3.5,3.1,4.5,5.5,1.2,5.8]
,'col2':[2.5,2.45,2.75,1.75,1.55,2.25,2.75,0.6,2.9]
,'col3':[NaN,2.45,2.75,2.75,2.75,2.75,2.75,2.75,2.9]
})
if df[‘col3’][-1] >= setpoint:
self.log(‘setpoint hit')
return True
Cummax 解决方案需要调整:col3 应该评估 col2 和 col3 的基础值,一旦违反设定值(col3 为 2.71),下一个 col3 值应重置为 NaN 并开始新的 cummax。 col3 的正确输出应该是:[NaN,2.45,2.75,NaN,1.55,2.25,2.75,NaN,2.9] 和 return 当 col3 的最后一行违反设定值 2.71 时一次又一次为真。
操作 cummax 和对 col3 进行额外调整的预期结果(可能与引用 col2 的 groupby 一起使用?):return 每次违反设定点时都为真。这是生成的 col3 的一个示例:
col0 col1 col2 col3
0 1 5.0 2.50 NaN
1 2 4.9 2.45 2.45
2 3 5.5 2.75 2.75
3 4 3.5 1.75 NaN
4 5 3.1 1.55 1.55
5 6 4.5 2.25 2.25
6 7 5.5 2.75 2.75
7 8 1.2 0.60 NaN
8 9 5.8 2.90 2.90
接受关于 NaN 是在发生违规的行还是在下一行如上所示的 return 的建议(主要希望 if 语句在违反设定值时立即解析为 True)。
尝试:
import pandas as pd
import numpy as np
df = pd.DataFrame({'col0':[1,2,3,4,5,6,7,8,9]
,'col1':[5,4.9,5.5,3.5,3.1,4.5,5.5,1.2,5.8]
,'col2':[2.5,2.45,2.75,1.75,1.55,2.25,2.75,0.6,2.9]
,'col3':[np.nan,2.45,2.75,2.75,2.75,2.75,2.75,2.75,2.9]
})
threshold = 2.71
grp = df['col2'].ge(threshold).cumsum().shift().bfill()
df['col3'] = df['col2'].groupby(grp).transform(lambda x: x.shift(-1).cummax().shift())
print(df)
输出:
col0 col1 col2 col3
0 1 5.0 2.50 NaN
1 2 4.9 2.45 2.45
2 3 5.5 2.75 2.75
3 4 3.5 1.75 NaN
4 5 3.1 1.55 1.55
5 6 4.5 2.25 2.25
6 7 5.5 2.75 2.75
7 8 1.2 0.60 NaN
8 9 5.8 2.90 2.90
详情:
使用大于或等于阈值创建分组,然后使用带变换的 groupby 将相同的逻辑应用于数据帧中的每个组。
为了清楚起见,我修改了问题并删除了伪影和不一致之处 - 请重新打开以供社区考虑。一位贡献者已经认为将 groupby 与 cummax 结合使用可能是一种解决方案。
我有一个数据框,其中 col3 的先验值和 col2 的当前值之间的最大值是通过 Scott Boston 最近提供的 cummax 函数计算的(谢谢!),如下所示:
df['col3'] = df['col2'].shift(-1).cummax().shift().
生成的数据框如下所示。还添加了将 col2 与作为浮点类型值结果的设定值进行比较的所需逻辑。
运行cummax的结果:
col0 col1 col2 col3
0 1 5.0 2.50 NaN
1 2 4.9 2.45 2.45
2 3 5.5 2.75 2.75
3 4 3.5 1.75 2.75
4 5 3.1 1.55 2.75
5 6 4.5 2.25 2.75
6 7 5.5 2.75 2.75
7 8 1.2 0.6 2.75
8 9 5.8 2.90 2.90
希望在上例中当 col3 >= setpoint 或 2.71 时标记为 True,这样每次 col3 的最新行都超过 setpoint。
问题:当达到设定点时,cummax 解决方案不会重置。需要一个解决方案,在每次违反设定点时重置 cummax 计算。例如在上面的table中,在col3超过设定值时第一次True之后,即col2值为2.75,第二次应该满足相同的条件,即扩展数据中显示table 我在第 4 行中删除了 col3 的值,以说明需要“重置”cummax 计算。在 if 语句中,我使用下标 [-1] 来定位 df 中的最后一行(即最新行)。注意:col2=col1的当前值*constant1 where constant1 == 0.5
到目前为止尝试过的代码(注意 col3 没有正确重置):
if self.constant is not None: setpoint = self.constant * (1-self.temp) # suppose setpoint == 2.71
df = pd.DataFrame({'col0':[1,2,3,4,5,6,7,8,9]
,'col1':[5,4.9,5.5,3.5,3.1,4.5,5.5,1.2,5.8]
,'col2':[2.5,2.45,2.75,1.75,1.55,2.25,2.75,0.6,2.9]
,'col3':[NaN,2.45,2.75,2.75,2.75,2.75,2.75,2.75,2.9]
})
if df[‘col3’][-1] >= setpoint:
self.log(‘setpoint hit')
return True
Cummax 解决方案需要调整:col3 应该评估 col2 和 col3 的基础值,一旦违反设定值(col3 为 2.71),下一个 col3 值应重置为 NaN 并开始新的 cummax。 col3 的正确输出应该是:[NaN,2.45,2.75,NaN,1.55,2.25,2.75,NaN,2.9] 和 return 当 col3 的最后一行违反设定值 2.71 时一次又一次为真。
操作 cummax 和对 col3 进行额外调整的预期结果(可能与引用 col2 的 groupby 一起使用?):return 每次违反设定点时都为真。这是生成的 col3 的一个示例:
col0 col1 col2 col3
0 1 5.0 2.50 NaN
1 2 4.9 2.45 2.45
2 3 5.5 2.75 2.75
3 4 3.5 1.75 NaN
4 5 3.1 1.55 1.55
5 6 4.5 2.25 2.25
6 7 5.5 2.75 2.75
7 8 1.2 0.60 NaN
8 9 5.8 2.90 2.90
接受关于 NaN 是在发生违规的行还是在下一行如上所示的 return 的建议(主要希望 if 语句在违反设定值时立即解析为 True)。
尝试:
import pandas as pd
import numpy as np
df = pd.DataFrame({'col0':[1,2,3,4,5,6,7,8,9]
,'col1':[5,4.9,5.5,3.5,3.1,4.5,5.5,1.2,5.8]
,'col2':[2.5,2.45,2.75,1.75,1.55,2.25,2.75,0.6,2.9]
,'col3':[np.nan,2.45,2.75,2.75,2.75,2.75,2.75,2.75,2.9]
})
threshold = 2.71
grp = df['col2'].ge(threshold).cumsum().shift().bfill()
df['col3'] = df['col2'].groupby(grp).transform(lambda x: x.shift(-1).cummax().shift())
print(df)
输出:
col0 col1 col2 col3
0 1 5.0 2.50 NaN
1 2 4.9 2.45 2.45
2 3 5.5 2.75 2.75
3 4 3.5 1.75 NaN
4 5 3.1 1.55 1.55
5 6 4.5 2.25 2.25
6 7 5.5 2.75 2.75
7 8 1.2 0.60 NaN
8 9 5.8 2.90 2.90
详情:
使用大于或等于阈值创建分组,然后使用带变换的 groupby 将相同的逻辑应用于数据帧中的每个组。