繁体   English   中英

从数据框中提取高低数据

[英]Extract high and low data from a dataframe

这是我正在处理的 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM