BITCOIN

[bitcoin] pandas dataframe 제어

mle21 2022. 8. 22. 06:00
반응형

pyupbit를 통해서 1분봉을 조회할 경우 200개의 데이터가 pandas dataframe 형식으로 넘어온다. 내가 원하는 부분만 싹싹 뽑아서 분석해보도록 하자.


0. 1분봉 조회

import pyupbit

#minute1 = pyupbit.get_ohlcv("KRW-BTC", "minute1") # 기본 200개 조회
minute1 = pyupbit.get_ohlcv("KRW-BTC", "minute1", 5) # 5개 조회
print(minute1)

#                           open        high         low       close    volume         value
#2023-02-18 17:39:00  31752000.0  31766000.0  31750000.0  31750000.0  4.719246  1.498646e+08
#2023-02-18 17:40:00  31756000.0  31766000.0  31750000.0  31750000.0  1.627533  5.167744e+07
#2023-02-18 17:41:00  31748000.0  31750000.0  31748000.0  31749000.0  1.662913  5.279563e+07
#2023-02-18 17:42:00  31749000.0  31770000.0  31748000.0  31765000.0  3.246643  1.030999e+08
#2023-02-18 17:43:00  31770000.0  31780000.0  31765000.0  31772000.0  4.014157  1.275467e+08

 

1. 행 label 확인 : df.index

print(minute1.index)

#DatetimeIndex(['2023-02-18 17:50:00', '2023-02-18 17:51:00',
#               '2023-02-18 17:52:00', '2023-02-18 17:53:00',
#               '2023-02-18 17:54:00'],
#              dtype='datetime64[ns]', freq=None)

 

2. 열 label 확인 : df.columns

print(minute1.columns)

#Index(['open', 'high', 'low', 'close', 'volume', 'value'], dtype='object')

 

3. 데이터 앞에서 x개 출력 : df.head(x)

print(minute1.head(3))

#                           open        high         low       close    volume         value
#2023-02-18 17:53:00  31782000.0  31787000.0  31767000.0  31785000.0  4.461434  1.417950e+08
#2023-02-18 17:54:00  31787000.0  31790000.0  31766000.0  31767000.0  2.860694  9.091389e+07
#2023-02-18 17:55:00  31787000.0  31790000.0  31746000.0  31785000.0  7.896699  2.509108e+08

 

4. 데이터 뒤에서 x개 출력 : df.tail(x)

print(minute1.tail(3))

#                           open        high         low       close    volume         value
#2023-02-18 17:58:00  31764000.0  31767000.0  31750000.0  31764000.0  3.846803  1.221681e+08
#2023-02-18 17:59:00  31760000.0  31783000.0  31750000.0  31779000.0  5.452941  1.731655e+08
#2023-02-18 18:00:00  31776000.0  31778000.0  31754000.0  31778000.0  1.551035  4.928569e+07

 

5. 특정 열 전체 값 추출 : df['열 이름']

print(minute1['open'])

2023-02-18 18:02:00    31760000.0
2023-02-18 18:03:00    31778000.0
2023-02-18 18:04:00    31777000.0
2023-02-18 18:05:00    31763000.0
2023-02-18 18:06:00    31759000.0
Name: open, dtype: float64

 

6. 특정 열의 특정 값 추출 : df['열 이름'][row_index]

print(minute1['open'][0])

#31759000.0

 

7. 특정 행 전체 값 추출 : df.iloc[row_index] - column index가 DatetimeIndex일 경우 loc를 쓰면 에러

print(minute1.iloc[:3])

#                           open        high         low       close    volume         value
#2023-02-18 18:15:00  31743000.0  31743000.0  31738000.0  31742000.0  1.646953  5.227714e+07
#2023-02-18 18:16:00  31742000.0  31742000.0  31735000.0  31738000.0  2.873312  9.119798e+07
#2023-02-18 18:17:00  31738000.0  31739000.0  31728000.0  31728000.0  3.470897  1.101457e+08

 

8. 특정 행의 특정 값 추출 : df.iloc[row_index, column_index]

print(minute1.iloc[:3, 0])

#2023-02-18 18:18:00    31728000.0
#2023-02-18 18:19:00    31734000.0
#2023-02-18 18:20:00    31730000.0
#Name: open, dtype: float64
728x90

'BITCOIN' 카테고리의 다른 글

[bitcoin] pyupbit 함수 소개_2  (0) 2022.08.21
[bitcoin] pyupbit 함수 소개_1  (0) 2022.08.20