Pandas 应用条件语句:将 x 与滚动的平均值进行比较 window
Pandas apply conditional statement : compare x to the mean of the rolling window
尝试使用 pandas apply 函数将 A 的每个值与其 window 的平均值进行比较。
当前代码实现:
m = df.apply(lambda x: 1 if x['A'] > x.shift(3)['MEAN OF WINDOW'] else 0, axis = 1)
不确定这是否真的有效,因为它需要很长时间才能执行。我一定是做错了什么
实际数据框视图
您可以查看 bfill
(df['A'] > df['MEAN OF WINDOW'].bfill()).astype(int)
尝试使用 pandas apply 函数将 A 的每个值与其 window 的平均值进行比较。
当前代码实现:
m = df.apply(lambda x: 1 if x['A'] > x.shift(3)['MEAN OF WINDOW'] else 0, axis = 1)
不确定这是否真的有效,因为它需要很长时间才能执行。我一定是做错了什么
实际数据框视图
您可以查看 bfill
(df['A'] > df['MEAN OF WINDOW'].bfill()).astype(int)