得到熊猫滚动窗口的最小值



我正在尝试获取pandas中三个连续单元格的最小值。计算时应考虑上下各一个单元格。

我已经尝试了scipy的argelextrema,但我感觉它不执行滚动窗口。

感谢这是一个疯狂的方法,但它没有像预期的那样执行。

def pivot_swing_low(df):
data = df.copy()
data['d1'] = data.Close.shift(-1)
data['d3'] = data.Close.shift(0)
data['d4'] = data.Close.shift(1)
data['minPL'] = data[['d1', 'd3', 'd4']].min(axis=1)
data['PL'] = np.where(data['minPL'] == data['d3'], data['d3'], "NaN")

data['recentPL'] = data.PL.shift(2).astype(float).fillna(method='ffill')
data = data.drop(columns=['d1', 'd3', 'd4'])
return data

它将始终捕获行号33,但对我来说行31也是相关的。

38.78 1671068699999 2022-12-15 01:44:59.999 NaN NaN -0.37 0.00 0.37 0.023571 0.054286 0.023125 0.057698 0.400805 28.612474 NaN NaN 38.78 38.78 39.1530 38.79 1671068999999 2012-12-15 01:49:59.999 NaN NaN 0.01 0.01 0.00 0.022857 0.054286 0.022188 0.053576 0.414137 29.285496 NaN NaN 38.48 NaN 39.15NaN -0.31 0.00 0.31 0.021429 0.076429 0.020603 0.071892 0.286583 22.274722 22.274722 NaN 38.48 38.48 38.7832 38.67 1671069599999 222 -12-15 01:59:59.999 NaN NaN 0.19 0.19 0.00 0.035000 0.074286 0.032703 0.066757 0.489878 32.880419 NaN NaN 38.37 NaN 38.7833 38.37 0.00 0.30 0.035000 0.093571 0.030367 0.083417 0.364036 26.688174 NaN NaN 38.37 38.37 38.48南南0.21 0.21 0.00 0.050000 0.090000 0.043198 0.077459 0.557687 35.802263南南38.37南南38.4835 38.70 1671070499999 2022-12-15 02:14:59.999 NaN NaN 0.12 0.12 0.00 0.058571 0.090000 0.048684 0.071926 0.676857 40.364625 NaN 40.364625 38.58 NaN 38.37

import pandas as pd
# Load the data into a dataframe
df = pd.read_csv('data.csv')
# Calculate the minimum of the current cell, the cell above, and the cell below
min_three_cells = df['value'].rolling(3, min_periods=1).min()
# View the results
print(min_three_cells)

这可能有帮助。

最新更新