Notice
Recent Posts
Recent Comments
Link
«   2024/04   »
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
Archives
Today
Total
관리 메뉴

차근차근

json 본문

대학교/Data

json

SWKo 2020. 1. 30. 20:28
20200130practice3
In [310]:
from IPython.core.display import display, HTML
display(HTML("<style> .container{width:90% !important;}</style>"))
In [160]:
# 다음 증권 정보에서 -> 특정 업체의 주가를 다운로드
# pandas를 이용해 분석
In [161]:
# 1. 다음 증권 사이트에서 데이터 수집
In [162]:
import requests
In [163]:
import time
In [164]:
url = "http://finance.daum.net/api/quote/A005930/days"
In [280]:
data = get_stock_data("A005930", 1)
if data:
    json_data = json.loads(req.text)
    stock_data = pd.DataFrame(json_data['data'])
    total_pages = int(json_data['totalPages'])
    for page in range(2, total_pages + 1):
        #다음 페이지 수집
        #time.sleep(1)
        data = get_stock_data("A005930", page)
        json_data = json.loads(data)
        stock_data = pd.concat([stock_data,pd.DataFrame(json_data['data'])]) #페이지 합쳐줌
stock_data
Out[280]:
symbolCode date tradePrice tradeTime change changePrice changeRate prevClosingPrice exchangeCountry openingPrice highPrice lowPrice accTradePrice accTradeVolume periodTradePrice periodTradeVolume
0 A005930 2020-01-30 15:30:23 57200.0 15:30:23 FALL 1900.0 -0.032149 59100.0 KOREA 58800.0 58800.0 56800.0 1195873843300 20699313 1195873843300 20699313
1 A005930 2020-01-29 15:30:24 59100.0 15:30:24 RISE 300.0 0.005102 58800.0 KOREA 59100.0 59700.0 58800.0 975344481100 16446102 975344481100 16446102
2 A005930 2020-01-28 15:30:29 58800.0 15:30:29 FALL 2000.0 -0.032895 60800.0 KOREA 59400.0 59400.0 58300.0 1394361672900 23664541 1394361672900 23664541
3 A005930 2020-01-23 15:30:12 60800.0 15:30:12 FALL 1500.0 -0.024077 62300.0 KOREA 61800.0 61800.0 60700.0 912319470070 14916555 912319470070 14916555
4 A005930 2020-01-22 15:30:01 62300.0 15:30:01 RISE 900.0 0.014658 61400.0 KOREA 60500.0 62600.0 60400.0 943882357729 15339565 943882357729 15339565
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
5 A005930 2020-01-21 15:30:09 61400.0 15:30:09 FALL 1000.0 -0.016026 62400.0 KOREA 62000.0 62400.0 61200.0 686991404500 11142693 686991404500 11142693
6 A005930 2020-01-20 15:30:03 62400.0 15:30:03 RISE 1100.0 0.017945 61300.0 KOREA 62000.0 62800.0 61700.0 781048125700 12528855 781048125700 12528855
7 A005930 2020-01-17 15:30:07 61300.0 15:30:07 RISE 600.0 0.009885 60700.0 KOREA 61900.0 62000.0 61000.0 984566747211 16025661 984566747211 16025661
8 A005930 2020-01-16 15:30:07 60700.0 15:30:07 RISE 1700.0 0.028814 59000.0 KOREA 59100.0 60700.0 59000.0 862500254132 14381774 862500254132 14381774
9 A005930 2020-01-15 15:30:06 59000.0 15:30:06 FALL 1000.0 -0.016667 60000.0 KOREA 59500.0 59600.0 58900.0 846497803148 14300928 846497803148 14300928

8900 rows × 16 columns

In [282]:
stock_data.head()
# 시고저종 - 시가, 고가, 저가, 종가
Out[282]:
symbolCode date tradePrice tradeTime change changePrice changeRate prevClosingPrice exchangeCountry openingPrice highPrice lowPrice accTradePrice accTradeVolume periodTradePrice periodTradeVolume
0 A005930 2020-01-30 15:30:23 57200.0 15:30:23 FALL 1900.0 -0.032149 59100.0 KOREA 58800.0 58800.0 56800.0 1195873843300 20699313 1195873843300 20699313
1 A005930 2020-01-29 15:30:24 59100.0 15:30:24 RISE 300.0 0.005102 58800.0 KOREA 59100.0 59700.0 58800.0 975344481100 16446102 975344481100 16446102
2 A005930 2020-01-28 15:30:29 58800.0 15:30:29 FALL 2000.0 -0.032895 60800.0 KOREA 59400.0 59400.0 58300.0 1394361672900 23664541 1394361672900 23664541
3 A005930 2020-01-23 15:30:12 60800.0 15:30:12 FALL 1500.0 -0.024077 62300.0 KOREA 61800.0 61800.0 60700.0 912319470070 14916555 912319470070 14916555
4 A005930 2020-01-22 15:30:01 62300.0 15:30:01 RISE 900.0 0.014658 61400.0 KOREA 60500.0 62600.0 60400.0 943882357729 15339565 943882357729 15339565
In [283]:
stock_data.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 8900 entries, 0 to 9
Data columns (total 16 columns):
symbolCode           8900 non-null object
date                 8900 non-null object
tradePrice           8900 non-null float64
tradeTime            8900 non-null object
change               8900 non-null object
changePrice          8900 non-null float64
changeRate           8900 non-null float64
prevClosingPrice     8900 non-null float64
exchangeCountry      8900 non-null object
openingPrice         8900 non-null float64
highPrice            8900 non-null float64
lowPrice             8900 non-null float64
accTradePrice        8900 non-null int64
accTradeVolume       8900 non-null int64
periodTradePrice     8900 non-null int64
periodTradeVolume    8900 non-null int64
dtypes: float64(7), int64(4), object(5)
memory usage: 1.2+ MB
In [284]:
# 1. 전처리 - 날짜 데이터가 날짜 형태가 아니라서 -> 형변환
# 원하는 열을 형변환 한다.
# 원하는 열을 선택해서 형변환 한다.
#stock_date.date
stock_data["date"]  = pd.to_datetime(stock_data["date"], format="%Y-%m-%d %H:%M:%S", errors="coerce")
In [286]:
stock_data["date"]
Out[286]:
0   2020-01-30 15:30:23
1   2020-01-29 15:30:24
2   2020-01-28 15:30:29
3   2020-01-23 15:30:12
4   2020-01-22 15:30:01
            ...        
5   2020-01-21 15:30:09
6   2020-01-20 15:30:03
7   2020-01-17 15:30:07
8   2020-01-16 15:30:07
9   2020-01-15 15:30:06
Name: date, Length: 8900, dtype: datetime64[ns]
In [171]:
# 2. 인덱스 설정
# * 판다스의 메서드들은 대부분 원본은 그대로 두고, 복사본을 준다.
# stock_data.set_index('date', inplace=True) # inplace -> 원본을 바꿔줌
# stock_data = stock_data.set_index('date')
# stock_data.index = stock_data['date']
In [287]:
stock_data.head()
Out[287]:
symbolCode date tradePrice tradeTime change changePrice changeRate prevClosingPrice exchangeCountry openingPrice highPrice lowPrice accTradePrice accTradeVolume periodTradePrice periodTradeVolume
0 A005930 2020-01-30 15:30:23 57200.0 15:30:23 FALL 1900.0 -0.032149 59100.0 KOREA 58800.0 58800.0 56800.0 1195873843300 20699313 1195873843300 20699313
1 A005930 2020-01-29 15:30:24 59100.0 15:30:24 RISE 300.0 0.005102 58800.0 KOREA 59100.0 59700.0 58800.0 975344481100 16446102 975344481100 16446102
2 A005930 2020-01-28 15:30:29 58800.0 15:30:29 FALL 2000.0 -0.032895 60800.0 KOREA 59400.0 59400.0 58300.0 1394361672900 23664541 1394361672900 23664541
3 A005930 2020-01-23 15:30:12 60800.0 15:30:12 FALL 1500.0 -0.024077 62300.0 KOREA 61800.0 61800.0 60700.0 912319470070 14916555 912319470070 14916555
4 A005930 2020-01-22 15:30:01 62300.0 15:30:01 RISE 900.0 0.014658 61400.0 KOREA 60500.0 62600.0 60400.0 943882357729 15339565 943882357729 15339565
In [288]:
# 3. 정렬하기
stock_data = stock_data.sort_index()
In [289]:
stock_data.head()
Out[289]:
symbolCode date tradePrice tradeTime change changePrice changeRate prevClosingPrice exchangeCountry openingPrice highPrice lowPrice accTradePrice accTradeVolume periodTradePrice periodTradeVolume
0 A005930 2020-01-30 15:30:23 57200.0 15:30:23 FALL 1900.0 -0.032149 59100.0 KOREA 58800.0 58800.0 56800.0 1195873843300 20699313 1195873843300 20699313
0 A005930 2020-01-30 15:30:23 57200.0 15:30:23 FALL 1900.0 -0.032149 59100.0 KOREA 58800.0 58800.0 56800.0 1199862738882 20768510 1199862738882 20768510
0 A005930 2020-01-30 15:30:23 57200.0 15:30:23 FALL 1900.0 -0.032149 59100.0 KOREA 58800.0 58800.0 56800.0 1199862738882 20768510 1199862738882 20768510
0 A005930 2020-01-30 15:30:23 57200.0 15:30:23 FALL 1900.0 -0.032149 59100.0 KOREA 58800.0 58800.0 56800.0 1199862738882 20768510 1199862738882 20768510
0 A005930 2020-01-30 15:30:23 57200.0 15:30:23 FALL 1900.0 -0.032149 59100.0 KOREA 58800.0 58800.0 56800.0 1199862738882 20768510 1199862738882 20768510
In [290]:
# 4. 이동평균 데이터 구하기
stock_data['tradePrice'].rolling(window=5).mean()
Out[290]:
0        NaN
0        NaN
0        NaN
0        NaN
0    57200.0
      ...   
9    59000.0
9    59000.0
9    59000.0
9    59000.0
9    59000.0
Name: tradePrice, Length: 8900, dtype: float64
In [291]:
# 5. 새로운 열을 만들어서 데이터 저장하기
stock_data['5일이평'] = stock_data['tradePrice'].rolling(window=5).mean()
In [292]:
# 5. 30일이평, 60일이평 계산해서 저장하기
stock_data['30일이평'] = stock_data['tradePrice'].rolling(window=30).mean()

stock_data['60일이평'] = stock_data['tradePrice'].rolling(window=60).mean()
In [293]:
stock_data
Out[293]:
symbolCode date tradePrice tradeTime change changePrice changeRate prevClosingPrice exchangeCountry openingPrice highPrice lowPrice accTradePrice accTradeVolume periodTradePrice periodTradeVolume 5일이평 30일이평 60일이평
0 A005930 2020-01-30 15:30:23 57200.0 15:30:23 FALL 1900.0 -0.032149 59100.0 KOREA 58800.0 58800.0 56800.0 1195873843300 20699313 1195873843300 20699313 NaN NaN NaN
0 A005930 2020-01-30 15:30:23 57200.0 15:30:23 FALL 1900.0 -0.032149 59100.0 KOREA 58800.0 58800.0 56800.0 1199862738882 20768510 1199862738882 20768510 NaN NaN NaN
0 A005930 2020-01-30 15:30:23 57200.0 15:30:23 FALL 1900.0 -0.032149 59100.0 KOREA 58800.0 58800.0 56800.0 1199862738882 20768510 1199862738882 20768510 NaN NaN NaN
0 A005930 2020-01-30 15:30:23 57200.0 15:30:23 FALL 1900.0 -0.032149 59100.0 KOREA 58800.0 58800.0 56800.0 1199862738882 20768510 1199862738882 20768510 NaN NaN NaN
0 A005930 2020-01-30 15:30:23 57200.0 15:30:23 FALL 1900.0 -0.032149 59100.0 KOREA 58800.0 58800.0 56800.0 1199862738882 20768510 1199862738882 20768510 57200.0 NaN NaN
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
9 A005930 2020-01-15 15:30:06 59000.0 15:30:06 FALL 1000.0 -0.016667 60000.0 KOREA 59500.0 59600.0 58900.0 846497803148 14300928 846497803148 14300928 59000.0 59000.0 59000.0
9 A005930 2020-01-15 15:30:06 59000.0 15:30:06 FALL 1000.0 -0.016667 60000.0 KOREA 59500.0 59600.0 58900.0 846497803148 14300928 846497803148 14300928 59000.0 59000.0 59000.0
9 A005930 2020-01-15 15:30:06 59000.0 15:30:06 FALL 1000.0 -0.016667 60000.0 KOREA 59500.0 59600.0 58900.0 846497803148 14300928 846497803148 14300928 59000.0 59000.0 59000.0
9 A005930 2020-01-15 15:30:06 59000.0 15:30:06 FALL 1000.0 -0.016667 60000.0 KOREA 59500.0 59600.0 58900.0 846497803148 14300928 846497803148 14300928 59000.0 59000.0 59000.0
9 A005930 2020-01-15 15:30:06 59000.0 15:30:06 FALL 1000.0 -0.016667 60000.0 KOREA 59500.0 59600.0 58900.0 846497803148 14300928 846497803148 14300928 59000.0 59000.0 59000.0

8900 rows × 19 columns

In [295]:
# 6. 단 > 장 - 단 < 장
# 6. 단 < 장 - 단 > 장
In [294]:
stock_data['5일이평_prev'] = stock_data['5일이평'].shift(1)
In [296]:
stock_data['30일이평_prev'] = stock_data['30일이평'].shift(1)
stock_data['60일이평_prev'] = stock_data['60일이평'].shift(1)
In [297]:
stock_data['5일이평_prev'] < stock_data['30일이평_prev']
Out[297]:
0    False
0    False
0    False
0    False
0    False
     ...  
9    False
9    False
9    False
9    False
9    False
Length: 8900, dtype: bool
In [298]:
# 열 데이터 비교하기
# 어제는 단기이평이 장기이평보다 낮고,
# 오늘은 단기이평이 장기이평보다 높다.
golden = (stock_data['5일이평_prev'] < stock_data['30일이평_prev']) & (stock_data['5일이평'] > stock_data['30일이평'])
dead = (stock_data['5일이평_prev'] > stock_data['30일이평_prev']) & (stock_data['5일이평'] < stock_data['30일이평'])
In [299]:
# 7. 원하는 조건에 맞는 행만 찾아보기
stock_data[golden == True]
Out[299]:
symbolCode date tradePrice tradeTime change changePrice changeRate prevClosingPrice exchangeCountry openingPrice ... accTradePrice accTradeVolume periodTradePrice periodTradeVolume 5일이평 30일이평 60일이평 5일이평_prev 30일이평_prev 60일이평_prev

0 rows × 22 columns

In [300]:
# 8. 처음으로 주가가 만원이 넘은날 찾기
stock_data[stock_data['tradePrice'] > 10000]
Out[300]:
symbolCode date tradePrice tradeTime change changePrice changeRate prevClosingPrice exchangeCountry openingPrice ... accTradePrice accTradeVolume periodTradePrice periodTradeVolume 5일이평 30일이평 60일이평 5일이평_prev 30일이평_prev 60일이평_prev
0 A005930 2020-01-30 15:30:23 57200.0 15:30:23 FALL 1900.0 -0.032149 59100.0 KOREA 58800.0 ... 1195873843300 20699313 1195873843300 20699313 NaN NaN NaN NaN NaN NaN
0 A005930 2020-01-30 15:30:23 57200.0 15:30:23 FALL 1900.0 -0.032149 59100.0 KOREA 58800.0 ... 1199862738882 20768510 1199862738882 20768510 NaN NaN NaN NaN NaN NaN
0 A005930 2020-01-30 15:30:23 57200.0 15:30:23 FALL 1900.0 -0.032149 59100.0 KOREA 58800.0 ... 1199862738882 20768510 1199862738882 20768510 NaN NaN NaN NaN NaN NaN
0 A005930 2020-01-30 15:30:23 57200.0 15:30:23 FALL 1900.0 -0.032149 59100.0 KOREA 58800.0 ... 1199862738882 20768510 1199862738882 20768510 NaN NaN NaN NaN NaN NaN
0 A005930 2020-01-30 15:30:23 57200.0 15:30:23 FALL 1900.0 -0.032149 59100.0 KOREA 58800.0 ... 1199862738882 20768510 1199862738882 20768510 57200.0 NaN NaN NaN NaN NaN
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
9 A005930 2020-01-15 15:30:06 59000.0 15:30:06 FALL 1000.0 -0.016667 60000.0 KOREA 59500.0 ... 846497803148 14300928 846497803148 14300928 59000.0 59000.0 59000.0 59000.0 59000.0 59000.0
9 A005930 2020-01-15 15:30:06 59000.0 15:30:06 FALL 1000.0 -0.016667 60000.0 KOREA 59500.0 ... 846497803148 14300928 846497803148 14300928 59000.0 59000.0 59000.0 59000.0 59000.0 59000.0
9 A005930 2020-01-15 15:30:06 59000.0 15:30:06 FALL 1000.0 -0.016667 60000.0 KOREA 59500.0 ... 846497803148 14300928 846497803148 14300928 59000.0 59000.0 59000.0 59000.0 59000.0 59000.0
9 A005930 2020-01-15 15:30:06 59000.0 15:30:06 FALL 1000.0 -0.016667 60000.0 KOREA 59500.0 ... 846497803148 14300928 846497803148 14300928 59000.0 59000.0 59000.0 59000.0 59000.0 59000.0
9 A005930 2020-01-15 15:30:06 59000.0 15:30:06 FALL 1000.0 -0.016667 60000.0 KOREA 59500.0 ... 846497803148 14300928 846497803148 14300928 59000.0 59000.0 59000.0 59000.0 59000.0 59000.0

8900 rows × 22 columns

In [301]:
stock_data['golden'] = golden
stock_data['dead'] = dead
In [302]:
import matplotlib.pyplot as plt
In [303]:
plt.rc('font', family = 'Times New Roman')
In [ ]:
plt.figure(dpi=120)
plt.plot(stock_data.index, stock_data['5일이평'], label="5일 이평")
plt.plot(stock_data.index, stock_data['30일이평'], label ="30일 이평")
plt.plot(stock_data.index, stock_data['60일이평'], label = "60일 이평")
# cross 발생 지점 표시하기
#plt.plot(stock_data.golden==True.index, stock_data['5일이평'][stock_data.golden==True],label="5일 이평")
# 데이터 범위
plt.xlim([10, 8000])
# 범례 표시
plt.legend(loc='best')
plt.xlabel("거래일")
plt.ylabel("증가")
plt.title("삼성전자 주가 그래프")
In [181]:
import numpy as np
In [182]:
np.nan # Not a Number -> 결측치
# 전처리 -> 결측치 처리
# 1. 결측치가 있는 데이터 (열, 행)
# 2. 특정 값으로 채운다.
# 2-1. 대표값으로 채운다 - 전체 데이터의 평균
# 2-2. 끌어다 채우기 -> 앞, 전 열(행)의 값을 끌어다 채우기
# 2-3. 유추값으로 채우기
np.inf #infinite
Out[182]:
inf
In [183]:
def get_stock_data(code, page=1):
    # 옵션 -> ?symbolCode=A005930&page=1&perPage=10&pagination=true
    options = {
        "symbolCode":"A005930",
        "page":"1",
        "perPage":"10",
        "pagination":"true"
    }

    custom_headers = {
        "referer":"http://finance.daum.net/quotes/A005930",
        "user-agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36"
    }
    req = requests.get(url, headers=custom_headers, params=options)
    if req.status_code == requests.codes.ok:
        return req.text
    else:
        return False
In [184]:
req.text
Out[184]:
'{"data":[{"symbolCode":"A005930","date":"2020-01-30 15:30:23","tradePrice":57200.0,"tradeTime":"15:30:23","change":"FALL","changePrice":1900.0,"changeRate":-0.0321489002,"prevClosingPrice":59100.0,"exchangeCountry":"KOREA","openingPrice":58800.0,"highPrice":58800.0,"lowPrice":56800.0,"accTradePrice":1195873843300,"accTradeVolume":20699313,"periodTradePrice":1195873843300,"periodTradeVolume":20699313},{"symbolCode":"A005930","date":"2020-01-29 15:30:24","tradePrice":59100.0,"tradeTime":"15:30:24","change":"RISE","changePrice":300.0,"changeRate":0.0051020408,"prevClosingPrice":58800.0,"exchangeCountry":"KOREA","openingPrice":59100.0,"highPrice":59700.0,"lowPrice":58800.0,"accTradePrice":975344481100,"accTradeVolume":16446102,"periodTradePrice":975344481100,"periodTradeVolume":16446102},{"symbolCode":"A005930","date":"2020-01-28 15:30:29","tradePrice":58800.0,"tradeTime":"15:30:29","change":"FALL","changePrice":2000.0,"changeRate":-0.0328947368,"prevClosingPrice":60800.0,"exchangeCountry":"KOREA","openingPrice":59400.0,"highPrice":59400.0,"lowPrice":58300.0,"accTradePrice":1394361672900,"accTradeVolume":23664541,"periodTradePrice":1394361672900,"periodTradeVolume":23664541},{"symbolCode":"A005930","date":"2020-01-23 15:30:12","tradePrice":60800.0,"tradeTime":"15:30:12","change":"FALL","changePrice":1500.0,"changeRate":-0.0240770465,"prevClosingPrice":62300.0,"exchangeCountry":"KOREA","openingPrice":61800.0,"highPrice":61800.0,"lowPrice":60700.0,"accTradePrice":912319470070,"accTradeVolume":14916555,"periodTradePrice":912319470070,"periodTradeVolume":14916555},{"symbolCode":"A005930","date":"2020-01-22 15:30:01","tradePrice":62300.0,"tradeTime":"15:30:01","change":"RISE","changePrice":900.0,"changeRate":0.0146579805,"prevClosingPrice":61400.0,"exchangeCountry":"KOREA","openingPrice":60500.0,"highPrice":62600.0,"lowPrice":60400.0,"accTradePrice":943882357729,"accTradeVolume":15339565,"periodTradePrice":943882357729,"periodTradeVolume":15339565},{"symbolCode":"A005930","date":"2020-01-21 15:30:09","tradePrice":61400.0,"tradeTime":"15:30:09","change":"FALL","changePrice":1000.0,"changeRate":-0.016025641,"prevClosingPrice":62400.0,"exchangeCountry":"KOREA","openingPrice":62000.0,"highPrice":62400.0,"lowPrice":61200.0,"accTradePrice":686991404500,"accTradeVolume":11142693,"periodTradePrice":686991404500,"periodTradeVolume":11142693},{"symbolCode":"A005930","date":"2020-01-20 15:30:03","tradePrice":62400.0,"tradeTime":"15:30:03","change":"RISE","changePrice":1100.0,"changeRate":0.0179445351,"prevClosingPrice":61300.0,"exchangeCountry":"KOREA","openingPrice":62000.0,"highPrice":62800.0,"lowPrice":61700.0,"accTradePrice":781048125700,"accTradeVolume":12528855,"periodTradePrice":781048125700,"periodTradeVolume":12528855},{"symbolCode":"A005930","date":"2020-01-17 15:30:07","tradePrice":61300.0,"tradeTime":"15:30:07","change":"RISE","changePrice":600.0,"changeRate":0.0098846787,"prevClosingPrice":60700.0,"exchangeCountry":"KOREA","openingPrice":61900.0,"highPrice":62000.0,"lowPrice":61000.0,"accTradePrice":984566747211,"accTradeVolume":16025661,"periodTradePrice":984566747211,"periodTradeVolume":16025661},{"symbolCode":"A005930","date":"2020-01-16 15:30:07","tradePrice":60700.0,"tradeTime":"15:30:07","change":"RISE","changePrice":1700.0,"changeRate":0.0288135593,"prevClosingPrice":59000.0,"exchangeCountry":"KOREA","openingPrice":59100.0,"highPrice":60700.0,"lowPrice":59000.0,"accTradePrice":862500254132,"accTradeVolume":14381774,"periodTradePrice":862500254132,"periodTradeVolume":14381774},{"symbolCode":"A005930","date":"2020-01-15 15:30:06","tradePrice":59000.0,"tradeTime":"15:30:06","change":"FALL","changePrice":1000.0,"changeRate":-0.0166666667,"prevClosingPrice":60000.0,"exchangeCountry":"KOREA","openingPrice":59500.0,"highPrice":59600.0,"lowPrice":58900.0,"accTradePrice":846497803148,"accTradeVolume":14300928,"periodTradePrice":846497803148,"periodTradeVolume":14300928}],"totalPages":890,"currentPage":1,"pageSize":10}'
In [185]:
import json
json_data = json.loads(req.text)
In [186]:
json_data
Out[186]:
{'data': [{'symbolCode': 'A005930',
   'date': '2020-01-30 15:30:23',
   'tradePrice': 57200.0,
   'tradeTime': '15:30:23',
   'change': 'FALL',
   'changePrice': 1900.0,
   'changeRate': -0.0321489002,
   'prevClosingPrice': 59100.0,
   'exchangeCountry': 'KOREA',
   'openingPrice': 58800.0,
   'highPrice': 58800.0,
   'lowPrice': 56800.0,
   'accTradePrice': 1195873843300,
   'accTradeVolume': 20699313,
   'periodTradePrice': 1195873843300,
   'periodTradeVolume': 20699313},
  {'symbolCode': 'A005930',
   'date': '2020-01-29 15:30:24',
   'tradePrice': 59100.0,
   'tradeTime': '15:30:24',
   'change': 'RISE',
   'changePrice': 300.0,
   'changeRate': 0.0051020408,
   'prevClosingPrice': 58800.0,
   'exchangeCountry': 'KOREA',
   'openingPrice': 59100.0,
   'highPrice': 59700.0,
   'lowPrice': 58800.0,
   'accTradePrice': 975344481100,
   'accTradeVolume': 16446102,
   'periodTradePrice': 975344481100,
   'periodTradeVolume': 16446102},
  {'symbolCode': 'A005930',
   'date': '2020-01-28 15:30:29',
   'tradePrice': 58800.0,
   'tradeTime': '15:30:29',
   'change': 'FALL',
   'changePrice': 2000.0,
   'changeRate': -0.0328947368,
   'prevClosingPrice': 60800.0,
   'exchangeCountry': 'KOREA',
   'openingPrice': 59400.0,
   'highPrice': 59400.0,
   'lowPrice': 58300.0,
   'accTradePrice': 1394361672900,
   'accTradeVolume': 23664541,
   'periodTradePrice': 1394361672900,
   'periodTradeVolume': 23664541},
  {'symbolCode': 'A005930',
   'date': '2020-01-23 15:30:12',
   'tradePrice': 60800.0,
   'tradeTime': '15:30:12',
   'change': 'FALL',
   'changePrice': 1500.0,
   'changeRate': -0.0240770465,
   'prevClosingPrice': 62300.0,
   'exchangeCountry': 'KOREA',
   'openingPrice': 61800.0,
   'highPrice': 61800.0,
   'lowPrice': 60700.0,
   'accTradePrice': 912319470070,
   'accTradeVolume': 14916555,
   'periodTradePrice': 912319470070,
   'periodTradeVolume': 14916555},
  {'symbolCode': 'A005930',
   'date': '2020-01-22 15:30:01',
   'tradePrice': 62300.0,
   'tradeTime': '15:30:01',
   'change': 'RISE',
   'changePrice': 900.0,
   'changeRate': 0.0146579805,
   'prevClosingPrice': 61400.0,
   'exchangeCountry': 'KOREA',
   'openingPrice': 60500.0,
   'highPrice': 62600.0,
   'lowPrice': 60400.0,
   'accTradePrice': 943882357729,
   'accTradeVolume': 15339565,
   'periodTradePrice': 943882357729,
   'periodTradeVolume': 15339565},
  {'symbolCode': 'A005930',
   'date': '2020-01-21 15:30:09',
   'tradePrice': 61400.0,
   'tradeTime': '15:30:09',
   'change': 'FALL',
   'changePrice': 1000.0,
   'changeRate': -0.016025641,
   'prevClosingPrice': 62400.0,
   'exchangeCountry': 'KOREA',
   'openingPrice': 62000.0,
   'highPrice': 62400.0,
   'lowPrice': 61200.0,
   'accTradePrice': 686991404500,
   'accTradeVolume': 11142693,
   'periodTradePrice': 686991404500,
   'periodTradeVolume': 11142693},
  {'symbolCode': 'A005930',
   'date': '2020-01-20 15:30:03',
   'tradePrice': 62400.0,
   'tradeTime': '15:30:03',
   'change': 'RISE',
   'changePrice': 1100.0,
   'changeRate': 0.0179445351,
   'prevClosingPrice': 61300.0,
   'exchangeCountry': 'KOREA',
   'openingPrice': 62000.0,
   'highPrice': 62800.0,
   'lowPrice': 61700.0,
   'accTradePrice': 781048125700,
   'accTradeVolume': 12528855,
   'periodTradePrice': 781048125700,
   'periodTradeVolume': 12528855},
  {'symbolCode': 'A005930',
   'date': '2020-01-17 15:30:07',
   'tradePrice': 61300.0,
   'tradeTime': '15:30:07',
   'change': 'RISE',
   'changePrice': 600.0,
   'changeRate': 0.0098846787,
   'prevClosingPrice': 60700.0,
   'exchangeCountry': 'KOREA',
   'openingPrice': 61900.0,
   'highPrice': 62000.0,
   'lowPrice': 61000.0,
   'accTradePrice': 984566747211,
   'accTradeVolume': 16025661,
   'periodTradePrice': 984566747211,
   'periodTradeVolume': 16025661},
  {'symbolCode': 'A005930',
   'date': '2020-01-16 15:30:07',
   'tradePrice': 60700.0,
   'tradeTime': '15:30:07',
   'change': 'RISE',
   'changePrice': 1700.0,
   'changeRate': 0.0288135593,
   'prevClosingPrice': 59000.0,
   'exchangeCountry': 'KOREA',
   'openingPrice': 59100.0,
   'highPrice': 60700.0,
   'lowPrice': 59000.0,
   'accTradePrice': 862500254132,
   'accTradeVolume': 14381774,
   'periodTradePrice': 862500254132,
   'periodTradeVolume': 14381774},
  {'symbolCode': 'A005930',
   'date': '2020-01-15 15:30:06',
   'tradePrice': 59000.0,
   'tradeTime': '15:30:06',
   'change': 'FALL',
   'changePrice': 1000.0,
   'changeRate': -0.0166666667,
   'prevClosingPrice': 60000.0,
   'exchangeCountry': 'KOREA',
   'openingPrice': 59500.0,
   'highPrice': 59600.0,
   'lowPrice': 58900.0,
   'accTradePrice': 846497803148,
   'accTradeVolume': 14300928,
   'periodTradePrice': 846497803148,
   'periodTradeVolume': 14300928}],
 'totalPages': 890,
 'currentPage': 1,
 'pageSize': 10}
In [187]:
json_data['totalPages']
Out[187]:
890
In [188]:
json_data['currentPage']
Out[188]:
1
In [189]:
import pandas as pd
In [190]:
stock_data = pd.DataFrame(json_data['data'])
In [191]:
stock_data.head()
Out[191]:
symbolCode date tradePrice tradeTime change changePrice changeRate prevClosingPrice exchangeCountry openingPrice highPrice lowPrice accTradePrice accTradeVolume periodTradePrice periodTradeVolume
0 A005930 2020-01-30 15:30:23 57200.0 15:30:23 FALL 1900.0 -0.032149 59100.0 KOREA 58800.0 58800.0 56800.0 1195873843300 20699313 1195873843300 20699313
1 A005930 2020-01-29 15:30:24 59100.0 15:30:24 RISE 300.0 0.005102 58800.0 KOREA 59100.0 59700.0 58800.0 975344481100 16446102 975344481100 16446102
2 A005930 2020-01-28 15:30:29 58800.0 15:30:29 FALL 2000.0 -0.032895 60800.0 KOREA 59400.0 59400.0 58300.0 1394361672900 23664541 1394361672900 23664541
3 A005930 2020-01-23 15:30:12 60800.0 15:30:12 FALL 1500.0 -0.024077 62300.0 KOREA 61800.0 61800.0 60700.0 912319470070 14916555 912319470070 14916555
4 A005930 2020-01-22 15:30:01 62300.0 15:30:01 RISE 900.0 0.014658 61400.0 KOREA 60500.0 62600.0 60400.0 943882357729 15339565 943882357729 15339565
In [192]:
stock_data.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 10 entries, 0 to 9
Data columns (total 16 columns):
symbolCode           10 non-null object
date                 10 non-null object
tradePrice           10 non-null float64
tradeTime            10 non-null object
change               10 non-null object
changePrice          10 non-null float64
changeRate           10 non-null float64
prevClosingPrice     10 non-null float64
exchangeCountry      10 non-null object
openingPrice         10 non-null float64
highPrice            10 non-null float64
lowPrice             10 non-null float64
accTradePrice        10 non-null int64
accTradeVolume       10 non-null int64
periodTradePrice     10 non-null int64
periodTradeVolume    10 non-null int64
dtypes: float64(7), int64(4), object(5)
memory usage: 1.4+ KB
In [193]:
# 바구니 객체 - 딕셔너리
# 사전형 객체 - 사전
# 키:값 - key:value
test_dict = {}
test_dict = dict()
test_dict = {
    "a":"apple",
    "b":"banana",
    "o":"orange",
    99:"hi",
    "heello":"greeting"
}
In [194]:
test_dict["b"] = "test"
In [195]:
test_dict # dict는 순서가 없습니다.
Out[195]:
{'a': 'apple', 'b': 'test', 'o': 'orange', 99: 'hi', 'heello': 'greeting'}
In [196]:
test_dict.update({"c":"carvena"})
In [197]:
test_dict
Out[197]:
{'a': 'apple',
 'b': 'test',
 'o': 'orange',
 99: 'hi',
 'heello': 'greeting',
 'c': 'carvena'}
In [198]:
test_dict["new"] = "new value"
In [199]:
test_dict
Out[199]:
{'a': 'apple',
 'b': 'test',
 'o': 'orange',
 99: 'hi',
 'heello': 'greeting',
 'c': 'carvena',
 'new': 'new value'}
In [200]:
# del - 메모리 해제
del(test_dict["c"])
In [201]:
test_dict
Out[201]:
{'a': 'apple',
 'b': 'test',
 'o': 'orange',
 99: 'hi',
 'heello': 'greeting',
 'new': 'new value'}
In [202]:
len(test_dict)
Out[202]:
6
In [203]:
"new" in test_dict
Out[203]:
True
In [204]:
# key 값 출력
for item in test_dict:
    print(item)
a
b
o
99
heello
new
In [205]:
# value 값 출력 (자주 쓰이는 형태)
for key in test_dict:
    print(test_dict[key])
apple
test
orange
hi
greeting
new value
In [206]:
test_dict.keys()
Out[206]:
dict_keys(['a', 'b', 'o', 99, 'heello', 'new'])
In [207]:
test_dict.values()
Out[207]:
dict_values(['apple', 'test', 'orange', 'hi', 'greeting', 'new value'])
In [208]:
# 자주 쓰이는 형태
for key, value in test_dict.items():
    print(key, value)
a apple
b test
o orange
99 hi
heello greeting
new new value

'대학교 > Data' 카테고리의 다른 글

matplotlib  (0) 2020.02.01
Kaggle [Titanic Data Analysis]  (0) 2020.02.01
pandas  (0) 2020.01.30
openpyxl  (0) 2020.01.30
Beautifulsoup  (0) 2020.01.30
Comments