파이썬/데이터프레임

python(vscode)/데이터프레임 최대/최소 구하기/첫줄/마지막줄

gongdol 2024. 2. 19. 22:18
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
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)
 
#3. 데이터 최대값 최소값 
print("\n#3.실행 - HIGH MAX\n",df['high'].max())
print("\n#3.실행 - LOW MIN\n",df['low'].min())
 
#4. 데이터 첫줄, 마지막줄 출력 
print("\n#4.실행 - open 첫줄\n",df.loc[df.index[0]]['open'])
print("\n#4.실행 - close 마지막줄\n",df.loc[df.index[-1]]['close'])
cs

 

 

2. 결과

300x250