如何将最近5天(日期)的最高高点和最低低点存储在变量中?
import pandas as pd
df = pd.DataFrame({'Date': [20210201,20210202,20210203,20210201,20210204,20210205], 'Open':
[426,428,426,440,425,431],'High': [429,429,428,450,427,433],'Low': [422,422,421,440,425,428],'Close':
[423,430,428,445,426,432]})
print(df)
结果应该是:
- hh = 450
- ll = 421
I did try with:
hh = df[df.High[::-1].cummax().eq(df.High)]
但是我不能解决这个任务,也不知道如何为最近5个日期(最近5天的最高高点)添加条件。谢谢您的支持
请参阅下面的工作代码:
# first sort data by Date
df = df.sort_values("Date", ascending=False)
hh = df.iloc[:5,:]["High"].max()
ll = df.iloc[:5,:]["Low"].min()