python - 从数据框中提取高低数据
问题描述
这是我正在处理的 data_frame
- Date Open High Low Close
1. 01-08-2019 | 97.85 | 98.45 | 96.40 |97.25
2. 02-08-2019 | 97.15 | 98.95 | 96.75 |98.15
3. 05-08-2019 | 98.30 | 98.70 | 94.30 |95.65
4. 06-08-2019 | 95.75 | 97.75 | 95.20 |97.05
5. 07-08-2019 | 96.80 | 97.70 | 96.05 |96.90
- ................
运行以下代码后:
new=data_frame.groupby(data_frame.index // 2).agg({'Date': 'last','High':'max','Low':'min'})
得到的结果低于DataFrame;
Date High Low
0 02-08-2019 98.95 96.40
1 06-08-2019 98.70 94.30
2 08-08-2019 98.90 96.05
3 12-08-2019 98.10 93.40
4 14-08-2019 96.60 93.15
我想以两天的高低并在第三天存储它。例如,01-08-2019 和 02-08-2019 的高低应在第 3 天(即 05-08-2019)比较存储。同样,应比较 02-08-2019 和 05-08-2019 以找到高低并将其存储在第三天,即 06-08-2019。应比较 05-08-2019 和 06-08-2019 以找到高低并在第三天存储,即 07-08-2019。
我上述解释的预期数据帧如下:
Date High Low
0 05-08-2019 98.95 96.40
1 06-08-2019 98.95 94.30
2 07-08-2019 98.70 94.30
解决方案
我们可以你下面的代码
x1=[]
x2=[]
x3=[]
count=0
date=data_frame['Date'].tolist()
high=data_frame['High'].tolist()
low=data_frame['Low'].tolist()
high.pop()
low.pop()
for i in range (len(data_frame)-2):
if count==0:
temp=pd.DataFrame()
date.remove(date[0])
temp['Date']=date
temp['High']=high
temp['Low']=low
count=1
else:
temp=pd.DataFrame()
date.remove(date[0])
temp['Date']=date
high.remove(high[0])
low.remove(low[0])
temp['High']=high
temp['Low']=low
TwoDay_High_Low=temp.groupby(temp.index // 2).agg({'Date': 'last','High':'max','Low':'min'})
x1.append(TwoDay_High_Low['Date'].iloc[0])
x2.append(TwoDay_High_Low['High'].iloc[0])
x3.append(TwoDay_High_Low['Low'].iloc[0])
最后制作一个数据框来存储提取的数据
two_day=pd.DataFrame()
two_day['Date']=x1
two_day['High']=x2
two_day['Low']=x3
two_day
推荐阅读
- go - 如何实现下面描述的功能
- javascript - 将 Hijir 日期添加到脚本中
- arrays - 为什么我在使用 nodeJS 和 yargs 时不断收到错误消息,提示我 JSON 输入意外结束
- python - DateTimeField 的 ArrayField 引发“django.db.utils.ProgrammingError: cannot cast type timestamp with time zone to timestamp with time zone[]”
- javascript - 如何根据输入值选择选项?
- html - 网页中的背景音频
- excel - 大工作簿的动态排序
- javascript - 调整事件监听器边缘的大小
- reactjs - 我如何检查用户输入是否为空(反应)
- matplotlib - 如何在 matplotlib 中计算曲线的最大值并将该值(最多小数点后两位)分配给带有箭头的曲线