초보 파이썬 4주차

<주식 종가 그래프 그리기>

import pandas as pd

code = pd.read_csv('./data/corpgeneral.csv')

code = code[['회사명', '종목코드']]

code_result = code.rename(columns={'회사명':'corp','종목코드':'code'})

corp_name = '카카오'
condition = "corp=='{}'".format(corp_name)

kakao = code_result.query(condition)
kakao = kakao['code']
kakao_string = kakao.to_string(index=False)
kakao_string = kakao_string.strip()
kakao_string = kakao_string.rjust(6,'0')
kakao_code = kakao_string + '.KS'

import pandas_datareader as pdr
kakao_stock_df = pdr.get_data_yahoo(kakao_code)

kakao_stock_df['Close'].plot()

<상관 관계 분석>

import pandas as pd
import pandas_datareader as pdr
from datetime import datetime

code = pd.read_csv('./data/corpgeneral.csv', header = 0)
code = code[['회사명', '종목코드']]
code_result = code.rename(columns={'회사명':'corp', '종목코드':'code'})

def get_code(code_result, corp_name):
    condition = "corp == '{}'".format(corp_name)
    code = code_result.query(condition)['code'].to_string(index=False)
    code = code.strip()
    code = code.rjust(6, '0')
    code = code + '.KS'
    return code
    
samsung_code = get_code(code_result, "삼성전자")
samsung_code

cj_code = get_code(code_result, "CJ")
cj_code

for company in companies:
    company_code = get_code(code_result, company)
    stock_df = pdr.get_data_yahoo(company_code, start, end)
    stocks_of_companies = stocks_of_companies.join(pd.DataFrame(stock_df['Close']).rename(columns={'Close':company}), on='Date')
    
corr_data = stocks_of_companies.corr()

import matplotlib.pyplot as plt
import seaborn as sns

plt.rcParams['font.family'] = 'AppleGothic'
plt.rcParams['axes.unicode_minus'] = False

conda install -c anaconda seaborn

plt.figure(figsize=(5,3))
sns.lineplot(data=kakao_stock_df['Close'])

plt.figure(figsize=(10,10))
sns.heatmap(data= corr_data, annot=True, fmt = '.2f', linewidths=.5,cmap='Blues')
plt.show()

좋은 웹페이지 즐겨찾기