파이썬/데이터프레임

python(vscode)/데이터프레임 시간 조건으로 출력하기

gongdol 2024. 2. 19. 22:21
300x250

1. 코드작성

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import random
import pandas as pd
import datetime
import time
 
ohlcv = {'min_t' : [], 'open':[],'high':[],'low':[],'close':[],'volume':[]}
now = datetime.datetime.now()
start_time = now
 
# 1. 랜덤데이터 만들기 
for i in range(5):
        now = datetime.datetime.now()
        min_t = now 
        open = random.randrange(1000022000)
        high = random.randrange(1000022000)
        low = random.randrange(1000022000)
        close = random.randrange(1000022000)
        volume = random.randrange(1000022000)
                
        # ohlcv['min_time'].append(min_time.strip())
        ohlcv['min_t'].append((min_t))
        ohlcv['open'].append(int(open))
        ohlcv['high'].append(int(high))
        ohlcv['low'].append(int(low))
        ohlcv['close'].append(int(close))
        ohlcv['volume'].append(int(volume))
        time.sleep(1)
 
#2. 데이터 프레임으로 변환
df = pd.DataFrame(ohlcv, columns=['min_t','open','high','low','close','volume'])
print("\n#2.실행\n",df)
 
#5. 시작 후 2초초과 4초미만 데이터만 가져온다.
first_time = start_time + datetime.timedelta(seconds= 2)
second_time = start_time + datetime.timedelta(seconds= 4)
print("\n#5. 실행 {} 와 {} 사이 데이터 출력".format(first_time,second_time))
for idx, t in enumerate(df['min_t']):
        if t > second_time:
                s = idx
        elif t > first_time:
                f = idx
 
print("\n#5.실행\n",df.iloc[f:s])
cs

 

 

2. 결과

 

300x250